-
-
Notifications
You must be signed in to change notification settings - Fork 362
[hoonjichoi1] WEEK 03 solutions #2716
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,35 @@ | ||
| /* | ||
| Time Complexity : O(c^t) | ||
| Space Complexity : O(t) | ||
| */ | ||
|
|
||
| class Solution { | ||
|
|
||
| public List<List<Integer>> combinationSum(int[] candidates, int target) { | ||
| List<List<Integer>> output = new ArrayList<>(); | ||
| Stack<Integer> nums = new Stack<>(); | ||
| dfs (candidates, output, target, nums, 0, 0); | ||
| return output; | ||
| } | ||
|
|
||
| private void dfs(int[] candidates, List<List<Integer>> output, int target, Stack<Integer> nums, int start, int total) { | ||
| // base case : pass | ||
| if (target == total) { | ||
| output.add(new ArrayList<>(nums)); | ||
| return; | ||
| } | ||
| // base case : fail | ||
| if (target < total) { | ||
| return; | ||
| } | ||
|
|
||
| for (int i = start ; i < candidates.length ; i++) { | ||
| int num = candidates[i]; | ||
| nums.push(num); | ||
| dfs(candidates, output, target, nums, i, total + num); | ||
| nums.pop(); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| } |
|
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 배열을 채웁니다. 0 처리와 경계 조건에 주의가 필요합니다. 개선 제안: 상수 공간으로 최적화 가능(두 변수만 사용하는 최적화). 예외 처리도 간단히 추가하면 안정적입니다.
|
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,25 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||||||||||||||
| dp[i] = dp[i-1] + dp[i-2] | ||||||||||||||||||||||||||||||||||||||||||||||
| Time Complexity : O(n) | ||||||||||||||||||||||||||||||||||||||||||||||
| Space Complexity : O(n) | ||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||
| class Solution { | ||||||||||||||||||||||||||||||||||||||||||||||
| public int numDecodings(String s) { | ||||||||||||||||||||||||||||||||||||||||||||||
| int n = s.length(); | ||||||||||||||||||||||||||||||||||||||||||||||
| int[] dp = new int[n + 1]; | ||||||||||||||||||||||||||||||||||||||||||||||
| dp[n] = 1; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| for (int i = n-1; i > -1; i--) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (s.charAt(i) != '0') { | ||||||||||||||||||||||||||||||||||||||||||||||
| dp[i] += dp[i+1]; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| if (s.charAt(i) != '0' && i+1 < n) { | ||||||||||||||||||||||||||||||||||||||||||||||
| int twoDigit = Integer.valueOf(s.substring(i, i+2)); | ||||||||||||||||||||||||||||||||||||||||||||||
| if (twoDigit > 0 && twoDigit < 27) { | ||||||||||||||||||||||||||||||||||||||||||||||
| dp[i] += dp[i+2]; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+12
to
+22
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.
Suggested change
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. 아 그렇네요! 피드백 감사합니다! |
||||||||||||||||||||||||||||||||||||||||||||||
| return dp[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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 입력 n의 이진 표현 길이만큼 반복하며 각 비트를 확인합니다. 개선 제안: n이 0일 때의 예외 처리와 루프 조건을 명확히 하면 더 안전합니다.
|
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,19 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class Solution { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public int hammingWeight(int n) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (n == 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int curr = n, result = 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while (curr > 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (curr % 2 == 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result++; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| curr = curr / 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return result; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+2
to
+17
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 == 1 처리를 별도로 해주지 않아도 될 거 같습니다!
Suggested change
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. 일부러 1이면 루프를 돌지 않게 base case를 추가한건데 없는게 더 효율적인 코드일까요?! |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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,23 @@ | ||||||
| class Solution { | ||||||
| public boolean isPalindrome(String s) { | ||||||
|
|
||||||
| // removing all non-alphanumeric characters and convert them to lower case | ||||||
| String conveted = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase(); | ||||||
|
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. 오타가 있네요
Suggested change
Comment on lines
+4
to
+5
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. 실제로 돌려보니 replaceAll 비용이 비싸서 runtime 성능이 좋지는 않게 나오는 거 같아요. 구현체도 참고차 남겨드려요! |
||||||
|
|
||||||
| // early return of the empty string case | ||||||
| if (conveted.length() == 0) { | ||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| // check the symmetry | ||||||
| int left = 0, right = conveted.length() - 1; | ||||||
| while (left <= right) { | ||||||
| if (conveted.charAt(left) != conveted.charAt(right)) { | ||||||
| return false; | ||||||
| } | ||||||
| left++; | ||||||
| right--; | ||||||
| } | ||||||
| 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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 백트래킹으로 모든 가능한 조합을 탐색하고 중복 없이 시작 위치를 고정하여 재귀를 진행합니다.
개선 제안: 현재 구현은 시간 복잡도가 후보 수와 목표합에 따라 크게 달라질 수 있습니다. 정렬 후 가지치기나 중복 제거를 추가하면 성능이 개선될 수 있습니다.