Skip to content
Merged
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
16 changes: 16 additions & 0 deletions climbing-stairs/solbijae.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function climbStairs(n: number): number {
if (n <= 3) return n;

// 첫 시도: 시간 복잡도 O(2^n), 공간 복잡도 O(n)
// return climbStairs(n - 1) + climbStairs(n - 2);

// 두번째 시도: 시간 복잡도 O(n), 공간 복잡도 O(1)
let prev1 = 2, prev2 = 1;
for (let i = 3; i <= n; i++) {
const curr = prev1 + prev2;
prev2 = prev1;
prev1 = curr;
}

return prev1;
};
19 changes: 19 additions & 0 deletions product-of-array-except-self/solbijae.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function productExceptSelf(nums: number[]): number[] {
// 시간 복잡도: O(n), 공간 복잡도: O(1)
const n = nums.length;
const answer = new Array(n).fill(1);

// answer[i]에 left 곱 저장
for (let i = 1; i < n; i++) {
answer[i] = answer[i - 1] * nums[i - 1];
}

// right 곱을 한 변수에 저장하면서 answer에 곱하기
let rightProduct = 1;
for (let i = n - 1; i >= 0; i--) {
answer[i] *= rightProduct;
rightProduct *= nums[i];
}

return answer;
}
17 changes: 17 additions & 0 deletions valid-anagram/solbijae.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function isAnagram(s: string, t: string): boolean {
// 첫 시도: 시간 복잡도 O(n log n), 공간 복잡도 O(n)
// const sSort = s.split('').sort();
// const tSort = t.split('').sort();
// return JSON.stringify(sSort) === JSON.stringify(tSort);

// 시간 복잡도 O(n), 공간복잡도 O(1)
if (s.length !== t.length) return false;

const count = new Array(26).fill(0);
for (let i=0; i<s.length; i++) {
count[s.charCodeAt(i) - 97]++;
count[t.charCodeAt(i) - 97]--;
}

return count.every(c => c === 0);
};