-
-
Notifications
You must be signed in to change notification settings - Fork 361
[jthw1005] WEEK02 solutions #2688
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| function threeSum(nums) { | ||
| const result = []; | ||
| nums.sort((a, b) => a - b); | ||
|
|
||
| for (let i = 0; i < nums.length - 2; i++) { | ||
| if (i > 0 && nums[i] === nums[i - 1]) continue; | ||
|
|
||
| let left = i + 1; | ||
| let right = nums.length - 1; | ||
|
|
||
| while (left < right) { | ||
| const sum = nums[i] + nums[left] + nums[right]; | ||
| if (sum === 0) { | ||
| result.push([nums[i], nums[left], nums[right]]); | ||
| left++; | ||
| right--; | ||
|
|
||
| while (left < right && nums[left] === nums[left - 1]) left++; | ||
| while (left < right && nums[right] === nums[right + 1]) right--; | ||
| } else if (sum < 0) { | ||
| left++; | ||
| } else { | ||
| right--; | ||
| } | ||
| } | ||
| } | ||
| return result; | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
생각해보니 메모이제이션 재귀라서 아주 비효율적이진 않네요. n이 작아서 지금 풀이로도 충분한 것 같아요.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 하나의 하위 문제만 재계산하지 않도록 캐시를 사용했다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| function climbStairs(n) { | ||
| const memo = {}; | ||
|
|
||
| function dp(k) { | ||
| if (k === 1) return 1; | ||
| if (k === 2) return 2; | ||
| if (memo[k]) return memo[k]; | ||
| memo[k] = dp(k - 1) + dp(k - 2); | ||
| return memo[k]; | ||
| } | ||
|
|
||
| return dp(n); | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 추가 배열을 사용해 좌우 누적곱을 분리 계산했다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| function productExceptSelf(nums) { | ||
| const result = []; | ||
| let product = 1; | ||
|
|
||
| for (let i = 0; i < nums.length; i++) { | ||
| result[i] = product; | ||
| product *= nums[i]; | ||
| } | ||
|
|
||
| product = 1; | ||
| for (let i = nums.length - 1; i >= 0; i--) { | ||
| result[i] *= product; | ||
| product *= nums[i]; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| console.log(productExceptSelf([1, 2, 3, 4])); |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
팔로업에 소문자 알파벳 외 문자열에 대해서도 처리할 수 있는 로직을 고민해보라고 적혀있습니다! 저도 알파벳 소문자만 한정해서 최적화하는 로직으로 작성해봐야겠네요 👍
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저는 함수 시작부에 길이 비교를 통해 성능을 조금 최적화해줬습니다. if (s.length !== t.length) {
return false;
}for가 2번 반복되는데, for문을 하나로 합치는 방법도 있을 것 같습니다! for (let i = 0; i < s.length; i++) {
data[s.charCodeAt(i) - a_ascii]++;
data[t.charCodeAt(i) - a_ascii]--;
}재밌는 풀이 잘 봤습니다 ㅎㅎ!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 소문자 알파벳이 아닌 조건이 추가되니까 더 생각할거리가 생기네요,, 감사합니다!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 알파벳 소문자만 다루는 가정하에 상수 공간으로 해결했다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| const isAnagram = (s, t) => { | ||
| const data = new Array(26).fill(0); | ||
| const a_ascii = 'a'.charCodeAt(); | ||
|
|
||
| for (const char of s) { | ||
| data[char.charCodeAt() - a_ascii]++; | ||
| } | ||
|
|
||
| for (const char of t) { | ||
| data[char.charCodeAt() - a_ascii]--; | ||
| } | ||
|
|
||
| return !data.some((el) => el !== 0); | ||
| }; |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 각 노드 방문 시 유효한 최소/최대 값을 넘겨 검증한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| function isValidBST(root) { | ||
| function helper(node, min, max) { | ||
| if (!node) return true; | ||
| if (node.val <= min || node.val >= max) return false; | ||
| return ( | ||
| helper(node.left, min, node.val) && helper(node.right, node.val, max) | ||
| ); | ||
| } | ||
| return helper(root, -Infinity, Infinity); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 정렬 뒤 i 중심으로 좌우 포인터를 이동하며 합이 0인 경우를 수집하고 중복을 건너뛴다.
개선 제안: 현재 구현이 적절해 보입니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저랑 같은 로직으로 작성하셨네요. 잘 봤습니다.
주석으로 공간/시간 복잡도 적어주시면 더 좋을 것 같습니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 잘 생각 안 하고 풀고 있었는데, 다음부턴 주석에 남겨보겠습니다! 👍