-
-
Notifications
You must be signed in to change notification settings - Fork 361
[xeulbn] WEEK 03 Solutions #2729
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,30 @@ | ||||||||||||||||||||||||||||||
| import java.util.*; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| class Solution { | ||||||||||||||||||||||||||||||
| public List<List<Integer>> combinationSum(int[] candidates, int target) { | ||||||||||||||||||||||||||||||
| List<List<Integer>> result = new ArrayList<>(); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| backtrack(candidates, target, 0, new ArrayList<>(), result); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return result; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| private void backtrack(int[] candidates, int remain, int start, List<Integer> current, List<List<Integer>> result) { | ||||||||||||||||||||||||||||||
| if (remain == 0) { | ||||||||||||||||||||||||||||||
| result.add(new ArrayList<>(current)); | ||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if (remain < 0) { | ||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| for (int i=start; i<candidates.length; i++) { | ||||||||||||||||||||||||||||||
| int candidate = candidates[i]; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| current.add(candidate); | ||||||||||||||||||||||||||||||
| backtrack(candidates, remain - candidate, i, current, result); | ||||||||||||||||||||||||||||||
| current.remove(current.size() - 1); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+22
to
+28
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. removeLast를 사용하시면 add -> remove 흐름을 좀더 직관적으로 나타낼 수 있을 거 것 같네요!
Suggested change
|
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 연속 부분 문제를 dp 배열로 해결하며 두 가지 상태만 고려한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,29 @@ | ||||||||||
| import java.util.*; | ||||||||||
|
|
||||||||||
| class Solution { | ||||||||||
| public int numDecodings(String s) { | ||||||||||
| int n = s.length(); | ||||||||||
| int[] dp = new int[n + 1]; | ||||||||||
|
|
||||||||||
| dp[0] = 1; | ||||||||||
|
|
||||||||||
| dp[1] = s.charAt(0) == '0' ? 0 : 1; | ||||||||||
|
|
||||||||||
| for (int i=2; i<=n; i++) { | ||||||||||
| char current = s.charAt(i - 1); | ||||||||||
|
|
||||||||||
| if (current != '0') { | ||||||||||
| dp[i] += dp[i - 1]; | ||||||||||
| } | ||||||||||
| int twoDigit = | ||||||||||
| (s.charAt(i - 2) - '0') * 10 | ||||||||||
| + (s.charAt(i - 1) - '0'); | ||||||||||
|
Comment on lines
+18
to
+20
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. substring 을 사용하면 좀 더 직관적일 수 있을 거 같아서 고려해보시면 좋을 거 같습니다 !
Suggested change
|
||||||||||
|
|
||||||||||
| if (twoDigit >= 10 && twoDigit <= 26) { | ||||||||||
| dp[i] += dp[i - 2]; | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| 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,21 @@ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| public int maxSubArray(int[] nums) { | ||
| int currentSum = nums[0]; | ||
| int maxSum = nums[0]; | ||
|
|
||
| for (int i=1; i<nums.length; i++) { | ||
| if (nums[i] > currentSum + nums[i]) { | ||
| currentSum = nums[i]; | ||
| } else { | ||
| currentSum += nums[i]; | ||
| } | ||
|
|
||
| if (currentSum > maxSum) { | ||
| maxSum = currentSum; | ||
| } | ||
| } | ||
| return maxSum; | ||
| } | ||
| } |
|
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의 비트 길이에 비례하는 시간으로 동작한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| public int hammingWeight(int n) { | ||
| int count = 0; | ||
|
|
||
| while (n > 0) { | ||
| if ((n & 1) == 1) { | ||
| count++; | ||
| } | ||
|
|
||
| n = n >> 1; | ||
| } | ||
|
|
||
| return count; | ||
| } | ||
| } | ||
|
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. 비트 연산을 활용해서 해결하셨군요! 찾아보니 이런 것을 Hamming이라고 하는 것 같네요 |
||
|
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,32 @@ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| public boolean isPalindrome(String s) { | ||
| int left =0; | ||
| int right = s.length()-1; | ||
|
|
||
| while (left < right) { | ||
|
|
||
| while (left < right && !Character.isLetterOrDigit(s.charAt(left))) { | ||
| left++; | ||
| } | ||
|
|
||
| while (left < right && !Character.isLetterOrDigit(s.charAt(right))) { | ||
| right--; | ||
| } | ||
|
|
||
| char leftChar = Character.toLowerCase(s.charAt(left)); | ||
| char rightChar = Character.toLowerCase(s.charAt(right)); | ||
|
|
||
| if (leftChar != rightChar) { | ||
| return false; | ||
| } | ||
|
|
||
| left++; | ||
| right--; | ||
| } | ||
|
|
||
|
|
||
| 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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 중복 제거를 위해 시작 인덱스를 유지하고 현재 부분해를 업데이트하며 백트래킹한다.
개선 제안: 현재 구현이 적절해 보입니다.