-
-
Notifications
You must be signed in to change notification settings - Fork 362
[j2h30728] WEEK 02 Solutions #2705
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
a7974dd
e2ff53f
dc633a3
f1528b4
3ca4fea
73c982d
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,46 @@ | ||
| /** | ||
| * 시간 복잡도 : O(n^2) | ||
| * 공간 복잡도 : O(1) | ||
| */ | ||
| function threeSum(nums: number[]): number[][] { | ||
| const result: number[][] = []; | ||
| nums.sort((a, b) => a - b); | ||
|
|
||
| for (let i = 0; i < nums.length - 2; i++) { | ||
| if (i > 0 && nums[i] === nums[i - 1]) { | ||
| continue; | ||
| } | ||
|
|
||
| if (nums[i] > 0) { | ||
| break; | ||
| } | ||
|
|
||
| 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 연속적인 합 문제를 두 변수로 유지하며 순차적으로 계산합니다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,18 @@ | ||
| /** | ||
| * 시간 복잡도 : O(n) | ||
| * 공간 복잡도 : O(1) | ||
| */ | ||
| function climbStairs(n: number): number { | ||
| if (n === 0 || n === 1) { | ||
| return 1; | ||
| } | ||
| if (n <= 2) return n; | ||
|
|
||
| const memo: number[] = []; | ||
| memo[0] = memo[1] = 1; | ||
| let prev2 = 1; | ||
| let prev1 = 2; | ||
| let current = 0; | ||
|
|
||
| for (let i = 2; i <= n; i++) { | ||
| memo[i] = memo[i - 1] + memo[i - 2]; | ||
| for (let i = 3; i <= n; i++) { | ||
| current = prev2 + prev1; | ||
| prev2 = prev1; | ||
| prev1 = current; | ||
| } | ||
|
|
||
| return memo[n]; | ||
| return current; | ||
| } |
|
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 |
|---|---|---|
| @@ -1,18 +1,21 @@ | ||
| /** | ||
| * 시간 복잡도 : O(n) | ||
| * 공간 복잡도 : O(1) (출력 배열 제외, left/right 변수만 사용) | ||
| */ | ||
| function productExceptSelf(nums: number[]): number[] { | ||
| const length = nums.length; | ||
| const result = new Array(nums.length).fill(1); | ||
| const arr = new Array(nums.length).fill(1); | ||
|
|
||
| let leftTemp = 1; | ||
| for (let i = 0; i < length; i++) { | ||
| result[i] = leftTemp; | ||
| leftTemp *= nums[i]; | ||
| let left = 1; | ||
| for (let i = 0; i < nums.length; i++) { | ||
| arr[i] = left; | ||
| left *= nums[i]; | ||
| } | ||
|
|
||
| let rightTemp = 1; | ||
| for (let i = length - 1; i >= 0; i--) { | ||
| result[i] *= rightTemp; | ||
| rightTemp *= nums[i]; | ||
| let right = 1; | ||
| for (let i = nums.length - 1; i >= 0; i--) { | ||
| arr[i] *= right; | ||
| right *= nums[i]; | ||
| } | ||
|
|
||
| return result; | ||
| return arr; | ||
| } |
|
Member
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. 공간 복잡도를 O(n)으로 적어주셨는데, 혹은 가능한 문자 집합의 크기를 k라고 본다면, 봇이 제안한 대로 O(k)도 맞는 표현인 듯합니다!
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. 오 감사합니다!
Member
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 |
|---|---|---|
| @@ -1,12 +1,26 @@ | ||
| /** | ||
| * | ||
| * @param s | ||
| * @param t | ||
| * @returns | ||
| * 시간 복잡도 : O(n) | ||
| * 공간 복잡도 : O(n) | ||
| */ | ||
| function isAnagram(s: string, t: string): boolean { | ||
| const obj = {}; | ||
| if (s.length !== t.length) return false; | ||
|
|
||
| const map = new Map<string, number>(); | ||
|
|
||
| for (let i = 0; i < s.length; i++) { | ||
| obj[s[i]] = (obj[s[i]] || 0) + 1; | ||
| map.set(s[i], (map.get(s[i]) ?? 0) + 1); | ||
| map.set(t[i], (map.get(t[i]) ?? 0) - 1); | ||
| } | ||
|
|
||
| for (let i = 0; i < t.length; i++) { | ||
| obj[t[i]] -= 1; | ||
| for (let ch of map.values()) { | ||
| if (ch !== 0) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return Object.values(obj).filter((value) => value !== 0).length === 0; | ||
| return true; | ||
| } |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 각 노드에 대해 허용 범위를 좁혀나가며 유효성 검사. 개선 제안: 현재 구현이 적절해 보입니다. |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 정렬과 두 포인터를 이용해 중복 제거를 처리하고 있으며 평균적으로 O(n^2) 시간이 소요됩니다.
개선 제안: 현재 구현이 적절해 보입니다.