-
-
Notifications
You must be signed in to change notification settings - Fork 363
[JeonJe] WEEK 03 Solutions #2713
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
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,34 @@ | ||||
| import java.util.*; | ||||
|
|
||||
| // TC: O(n^(target/min)) | ||||
| // SC: O(target/min) | ||||
| class Solution { | ||||
| private static List<List<Integer>> answer; | ||||
|
|
||||
| public List<List<Integer>> combinationSum(int[] candidates, int target) { | ||||
| answer = new ArrayList<>(); | ||||
|
|
||||
| List<Integer> bucket = new ArrayList<>(); | ||||
| int curSum = 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.
Suggested change
이 변수는 실제로 사용되지않는 데드코드인 것 같습니다. 없어도 될 것 같습니다! |
||||
|
|
||||
| dfs(candidates, bucket, target, 0, 0); | ||||
|
|
||||
| return answer; | ||||
| } | ||||
|
|
||||
| private void dfs(int[] candidates, List<Integer> bucket, int target, int startIdx, int curSum) { | ||||
| if (curSum == target) { | ||||
| answer.add(new ArrayList<>(bucket)); | ||||
| return; | ||||
| } | ||||
| if (curSum > target) | ||||
| return; | ||||
|
|
||||
| for (int i = startIdx; i < candidates.length; i++) { | ||||
| bucket.add(candidates[i]); | ||||
| dfs(candidates, bucket, target, i, curSum + candidates[i]); | ||||
| bucket.removeLast(); | ||||
| } | ||||
|
|
||||
| } | ||||
| } | ||||
|
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 @@ | ||
| import java.util.*; | ||
|
|
||
| // TC: O(n) | ||
| // SC: 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 = s.length() - 1; i >= 0; i--) { | ||
| if (s.charAt(i) != '0') { | ||
| dp[i] = dp[i + 1]; | ||
| } | ||
| if (i + 1 < n) { | ||
| int twoDigit = Integer.parseInt(s.substring(i, i + 2)); | ||
| if (twoDigit >= 10 && twoDigit <= 26) { | ||
| dp[i] += dp[i + 2]; | ||
| } | ||
| } | ||
| } | ||
| 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 각 위치에서 뒤의 부분배열 합과 현재 값을 비교해 최댓값을 갱신하는 방식 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,20 @@ | ||||||||||||||||||||||||||||||||||
| import java.util.*; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // TC: O(n) | ||||||||||||||||||||||||||||||||||
| // SC: O(n) | ||||||||||||||||||||||||||||||||||
| class Solution { | ||||||||||||||||||||||||||||||||||
| public int maxSubArray(int[] nums) { | ||||||||||||||||||||||||||||||||||
| int n = nums.length; | ||||||||||||||||||||||||||||||||||
| int[] dp = new int[n]; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| dp[n - 1] = nums[n - 1]; | ||||||||||||||||||||||||||||||||||
| int answer = dp[n - 1]; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| for (int i = n - 2; i >= 0; i--) { | ||||||||||||||||||||||||||||||||||
| dp[i] = Math.max(dp[i + 1] + nums[i], nums[i]); | ||||||||||||||||||||||||||||||||||
| answer = Math.max(answer, dp[i]); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+8
to
+16
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. 직관적으로 잘 구현하신 것 같습니다. 여기서 SC를 O(1)로 최적화해볼 수 있을 것 같습니다. 현재 점화식이 참조중인 값이 이전값을 담는 것 하나라서 dp[] 대신에 int 변수를 사용해도(현재 상태 하나만으로도) 구현이 가능합니다!
Suggested change
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return answer; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
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의 개수를 누적합니다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,17 @@ | ||||||||||
| import java.util.*; | ||||||||||
|
|
||||||||||
| // TC: O(1) | ||||||||||
| // SC: O(1) | ||||||||||
| class Solution { | ||||||||||
| public int hammingWeight(int n) { | ||||||||||
| int answer = 0; | ||||||||||
|
|
||||||||||
| while (n != 0) { | ||||||||||
| if ((n & 1) == 1) { | ||||||||||
| answer++; | ||||||||||
| } | ||||||||||
|
Comment on lines
+10
to
+12
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
|
||||||||||
| n = n >>> 1; | ||||||||||
| } | ||||||||||
| return answer; | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
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 @@ | ||
| import java.util.*; | ||
|
|
||
| // TC: O(n) | ||
| // SC: O(n) | ||
| class Solution { | ||
| public boolean isPalindrome(String s) { | ||
| StringBuilder sb = new StringBuilder(); | ||
|
|
||
| for (char c : s.toCharArray()) { | ||
| if (Character.isLetterOrDigit(c)) { | ||
| sb.append(Character.toLowerCase(c)); | ||
| } | ||
| } | ||
|
Comment on lines
+9
to
+13
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. 우와, 문자를 배열로 나눠서 하나씩 비교하는 거군요. 내장함수 |
||
|
|
||
| for (int i = 0; i < sb.length() / 2; i++) { | ||
| if (sb.charAt(i) != sb.charAt(sb.length() - 1 - i)) { | ||
| 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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 각 재귀 단계에서 남은 타깃을 초과하지 않도록 가지치기를 하고, 같은 원소를 재사용하도록 인덱스를 i로 고정된 상태에서 재귀합니다.
개선 제안: 현재 구현이 적절해 보입니다.