Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions combination-sum/juhui-jeong.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 시간 복잡도: O(2^(target / minCandidate))
// 공간 복잡도: O(target / minCandidate)
function combinationSum(candidates: number[], target: number): number[][] {
const result: number[][] = [];

const dfs = (index: number, remain: number, path: number[]) => {
if (remain === 0) {
result.push([...path]);
return;
}

if (remain < 0 || index === candidates.length) {
return;
}

const num = candidates[index];

path.push(num);
dfs(index, remain - num, path);
path.pop();

dfs(index + 1, remain, path);
};

dfs(0, target, []);

return result;
}
27 changes: 27 additions & 0 deletions number-of-1-bits/juhui-jeong.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//풀이 1
// 시간 복잡도: O(k)
// 공간 복잡도: O(1)
// 속도: 2ms
function hammingWeight(n: number): number {
let count = 0;
while (n) {
n = n & (n - 1);
count++;
}
return count;
}
/*
속도는 빠르지만 복잡도가 높기 때문에 적합하지 않음.
Brian Kernighan 알고리즘을 활용한 비트 계산으로 재풀이

// 시간 복잡도: O(log n)
// 공간 복잡도: O(log n)
// 속도: 0ms
function hammingWeight(n: number): number {
const bitNumber = n.toString(2);
const bitString = String(bitNumber);

const bitArray = bitString.split('').map((s) => Number(s));
return bitArray.reduce((a, b) => a + b);
}
*/
24 changes: 24 additions & 0 deletions valid-palindrome/juhui-jeong.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 시간 복잡도: O(n)
// 공간 복잡도: O(n)
function isPalindrome(s: string): boolean {
//1. s에서 문자와 숫자가 아닌 다른 것들은 제거
//2. 대문자 -> 소문자, 띄어쓰기, 공백 제거
let originalString = s;
let alphabeticString = originalString.replace(/[^a-zA-Z0-9]/g, '');
const filteredString = alphabeticString.toLowerCase().trim();

//3. 제거 후 length === 0 일 경우 true로 결과 값 반환.
if (filteredString.length === 0) {
return true;
}

let count = 0;
let roundedStringLength = Math.round(filteredString.length / 2);
for (let i = 0; i < roundedStringLength; i++) {
// 양 끝의 글자가 동일한지 체크하고
if (filteredString[i] === filteredString[filteredString.length - i - 1]) {
count += 1;
}
}
return count === roundedStringLength ? true : false;
}