Skip to content

[choidabom] WEEK 03 solutions #1311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 21, 2025
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/choidabom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// https://leetcode.com/problems/climbing-stairs/

// TC: O(N)
// SC: O(N)

var climbStairs = function (n) {
const stairs = [1, 2];

for (let i = 2; i < n; i++) {
stairs[i] = stairs[i - 1] + stairs[i - 2];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dp로 푸니 코드가 좀 더 깔끔하네요! 참고하겠습니다.

}

return stairs[n - 1];
};

console.log(climbStairs(5));
32 changes: 32 additions & 0 deletions maximum-subarray/choidabom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// https://leetcode.com/problems/maximum-subarray/

// Time Limit Exceeded
function maxSubArray(nums: number[]): number {
const acc = []
const len = nums.length

for (let size = 1; size <= len; size++) {
for (let start = 0; start <= len - size; start++) {
const sub = nums.slice(start, start + size)
const sum = sub.reduce((acc, num)=> acc += num, 0)
acc.push(sum)
}
}

return acc.sort((a, b) => b - a)[0]
};

// TC: O(n)
// SC: O(n)

function maxSubArray(nums: number[]): number {
const dp = [...nums];
let max = dp[0];

for (let i = 1; i < nums.length; i++) {
dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]);
max = Math.max(max, dp[i]);
}

return max;
}
15 changes: 15 additions & 0 deletions number-of-1-bits/choidabom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// https://leetcode.com/problems/number-of-1-bits/

// TC: O(logN) - n을 2로 나누며 1을 셈
// SC: O(1) - 배열을 사용하지 않고, 변수만 사용하여 추가 메모리 x

function hammingWeight(n: number): number {
let answer = 0

while (n > 0) {
answer += n % 2
n = Math.floor(n / 2)
}

return answer
}
33 changes: 33 additions & 0 deletions valid-anagram/choidabom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// https://leetcode.com/problems/valid-anagram/submissions/1603502655/

// TC: O(NlogN)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런 식으로 시간 복잡도 적으면 서 하는 것 좋네요! 참고하겠습니다.

// SC: O(N)

var isAnagram = function (s, t) {
return s.split("").sort().join("") === t.split("").sort().join("")
};

// TC: O(N)
// SC: O(N)

var isAnagram = function (s, t) {
const map = new Map()

for (const char of s) {
if (map.has(char)) map.set(char, map.get(char) + 1)
else map.set(char, 1)
}

for (const char of t) {
if (!map.has(char)) return false
else map.set(char, map.get(char) - 1)
}

for (const value of map.values()) {
if (value !== 0) return false
}

return true
};

console.log(isAnagram(s = "anagram", t = "nagaram"))
11 changes: 11 additions & 0 deletions valid-palindrome/choidabom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// https://leetcode.com/problems/valid-palindrome/

// TC: O(n)
// SC: O(n)

function isPalindrome(s: string): boolean {
const str = (s.toLowerCase().match(/[a-z0-9]/g) || []).join("");
const reverse = str.split("").reverse().join("");

return str === reverse;
}