-
-
Notifications
You must be signed in to change notification settings - Fork 362
[j2h30728] WEEK 03 Solutions #2736
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
base: main
Are you sure you want to change the base?
Changes from all commits
86fccd0
965f5e1
817cf7c
ba1bf83
0444130
a857901
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 @@ | ||
| /** | ||
| * 시간 복잡도: O(k * n^k) (n: candidates 길이, k: target / min(candidates) 최대 깊이) | ||
| * 공간 복잡도: O(k) | ||
| */ | ||
| function combinationSum(candidates: number[], target: number): number[][] { | ||
| const result: number[][] = []; | ||
| const current: number[] = []; | ||
|
|
||
| function backtracking(index: number, remain: number) { | ||
| if (remain === 0) { | ||
| result.push([...current]); | ||
| return; | ||
| } | ||
|
|
||
| if (remain < 0) { | ||
| return; | ||
| } | ||
|
|
||
| for (let i = index; i < candidates.length; i++) { | ||
| current.push(candidates[i]); | ||
| backtracking(i, remain - candidates[i]); | ||
| current.pop(); | ||
| } | ||
| } | ||
|
|
||
| backtracking(0, target); | ||
| 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 이전 두 상태만 필요하므로 DP 배열을 사용해 풀이가 안정적입니다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /** | ||
| * 시간 복잡도: O(n) | ||
| * 공간 복잡도: O(n) | ||
| */ | ||
| function numDecodings(s: string): number { | ||
| const dp = new Array(s.length + 1).fill(0); | ||
| dp[0] = 1; | ||
| dp[1] = parseInt(s[0]) === 0 ? 0 : 1; | ||
|
|
||
| for (let i = 2; i <= s.length; i++) { | ||
| dp[i] = 0; | ||
|
|
||
| if (parseInt(s[i - 1]) !== 0) { | ||
| dp[i] += dp[i - 1]; | ||
| } | ||
| if ( | ||
| parseInt(s.slice(i - 2, i)) <= 26 && | ||
| parseInt(s.slice(i - 2, i)) >= 10 | ||
| ) { | ||
| dp[i] += dp[i - 2]; | ||
| } | ||
| } | ||
| return dp[s.length]; | ||
| } |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
|
| 유저 분석 | 실제 분석 | 결과 | |
|---|---|---|---|
| Time | O(n) | O(n) | ✅ |
| Space | O(n) | O(n) | ✅ |
피드백: Kadane 방식과 유사한 DP 접근으로 각 위치의 최댓값을 유지합니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2: maxSubArray2 — Time: ✅ O(n) → O(n) / Space: ✅ O(1) → O(1)
| 유저 분석 | 실제 분석 | 결과 | |
|---|---|---|---|
| Time | O(n) | O(n) | ✅ |
| Space | O(1) | O(1) | ✅ |
피드백: 가장 일반적인 최적해로, 메모리 사용을 줄인 버전입니다.
개선 제안: 현재 구현이 적절해 보입니다.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /** | ||
| * 시간 복잡도: O(n) | ||
| * 공간 복잡도: O(n) | ||
| */ | ||
| function maxSubArray1(nums: number[]): number { | ||
| const dp = new Array(nums.length).fill(0); | ||
| dp[0] = nums[0]; | ||
|
|
||
| for (let i = 1; i < nums.length; i++) { | ||
| dp[i] += Math.max(dp[i - 1] + nums[i], nums[i]); | ||
| } | ||
| return Math.max(...dp); | ||
| } | ||
|
|
||
| /** | ||
| * 시간 복잡도: O(n) | ||
| * 공간 복잡도: O(1) | ||
| */ | ||
| function maxSubArray2(nums: number[]): number { | ||
| let max = nums[0]; | ||
| let current = nums[0]; | ||
|
|
||
| for (let i = 1; i < nums.length; i++) { | ||
| current = Math.max(current + nums[i], nums[i]); | ||
| max = Math.max(max, current); | ||
| } | ||
| return max; | ||
| } |
|
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 @@ | ||
| /** | ||
| * 시간 복잡도: O(n) (n: 입력값의 비트 수) | ||
| * 공간 복잡도: O(1) | ||
| */ | ||
| function hammingWeight(n: number): number { | ||
| let sum = 0; | ||
|
|
||
| while (n > 0) { | ||
| sum += Math.abs(n % 2); | ||
|
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. abs 없이 |
||
| n = Math.floor(n / 2); | ||
| } | ||
| return (sum += 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,19 @@ | ||
| /** | ||
| * 시간 복잡도: O(n) | ||
| * 공간 복잡도: O(n) | ||
| */ | ||
| function isPalindrome(s: string): boolean { | ||
| let string = ""; | ||
| for (const ch of s) { | ||
| if ((ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z")) { | ||
| string += ch.toLowerCase(); | ||
| } | ||
| } | ||
|
Comment on lines
+6
to
+11
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. 조건이 alphanumeric 인데 알파벳만 처리해서 테스트 통과가 안되네요! 다시 풀어주셔야 할 거 같아요~ |
||
|
|
||
| for (let i = 0; i < string.length / 2; i++) { | ||
| if (string[i] !== string[string.length - i - 1]) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 백트래킹으로 모든 조합을 탐색하며, 각 재귀에서 남은 합을 감소시키고 현재 경로를 관리합니다.
개선 제안: 현재 구현이 일반적인 해법에 부합합니다.