-
-
Notifications
You must be signed in to change notification settings - Fork 195
[ready-oun] Week 03 solutions #1292
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import java.util.*; | ||
class Solution { | ||
public List<List<Integer>> combinationSum(int[] candidates, int target) { | ||
List<List<Integer>> result = new ArrayList<>(); | ||
List<Integer> temp = new ArrayList<>(); | ||
|
||
backtrack(candidates, target, 0, temp, result); | ||
return result; | ||
} | ||
private void backtrack(int[] candidates, int target, int start, List<Integer> temp, List<List<Integer>> result) { | ||
if (target < 0) return; | ||
if (target == 0) { | ||
result.add(new ArrayList<>(temp)); // deep copy | ||
return; | ||
} | ||
|
||
for (int i = start; i < candidates.length; i++) { | ||
temp.add(candidates[i]); | ||
backtrack(candidates, target - candidates[i], i, temp, result); | ||
temp.remove(temp.size() -1); | ||
} | ||
|
||
} | ||
} | ||
|
||
/** | ||
Return all unique combinations where the candidate num sum to target | ||
- each num in cand[] can be used unlimited times | ||
- order of num in comb does NOT matter | ||
|
||
1. use backtracking to explore all possible comb | ||
2. backtrack, if current sum > target | ||
3. save comb, if current sum == target | ||
4. avoid dupl -> only consider num from crnt idx onward (no going back) | ||
|
||
Time: O(2^target) | ||
Space: O(target) | ||
|
||
Learned: Backtracking vs DFS | ||
- DFS: search all paths deeply (no conditions, no rollback). | ||
- Backtracking = DFS + decision making + undo step. | ||
Explore, prune (if invalid), save (if valid), then undo last choice. | ||
|
||
for (선택 가능한 숫자 하나씩) { | ||
선택하고 | ||
target 줄이고 (목표 가까워짐) | ||
재귀 호출로 다음 선택 | ||
실패하거나 성공하면 되돌리기 (백트래킹) | ||
} | ||
|
||
DFS just visits everything, | ||
Backtracking visits only what’s promising — and turns back if not! | ||
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class Solution { | ||
public int hammingWeight(int n) { | ||
int count = 0; | ||
|
||
while (n != 0) { | ||
if ((n & 1) == 1) { | ||
count++; | ||
} | ||
n = n >>> 1; | ||
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 count; | ||
} | ||
} | ||
|
||
/** | ||
Time: O(1) – max 32 iterations (fixed bit size) | ||
Space: O(1) | ||
|
||
How it works: Shift each bit → Check → Count → Shift again | ||
1. Shift each bit of n to the right | ||
2. Check if the last bit is 1 using n & 1 | ||
3. If it is, increment the count | ||
4. Shift n to the right using n = n >>> 1 | ||
|
||
Learned: | ||
(n & 1) isolates the least significant bit (LSB) to check if it’s 1 | ||
>> : Arithmetic shift (fills in sign bit, so infinite loop for negatives) | ||
>>> : Logical shift (always fills with 0, safe for negatives) | ||
Java evaluates == before &, so use parentheses to control the order | ||
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
class Solution { | ||
public boolean isPalindrome(String s) { | ||
StringBuilder cleaned = new StringBuilder(); | ||
|
||
for (char c : s.toCharArray()) { | ||
if (Character.isLetterOrDigit(c)) { | ||
cleaned.append(Character.toLowerCase(c)); | ||
} | ||
} | ||
|
||
int left = 0; | ||
int right = cleaned.length() - 1; | ||
while (left < right) { | ||
// Fail fast: return false as soon as a mismatch is found | ||
if (cleaned.charAt(left) != cleaned.charAt(right)) { | ||
return false; | ||
} | ||
left++; | ||
right--; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
/** | ||
converting all uppercase letters into lowercase letters | ||
removing all non-alphanumeric char | ||
|
||
1. cleaning | ||
0. str -> char with for-each loop | ||
1. check char if isLetterOrDigit | ||
2. make char to LowerCase | ||
* Character Class static method | ||
2. two ptrs comparison while left < right | ||
s[i] == s[n - 1 - i] | ||
|
||
- Time: O(n) | ||
|
||
/** REMEMBER | ||
1. length vs length() | ||
|
||
arr.length => field | ||
String(Builder).length() => method | ||
|
||
2. “fail fast” approach: | ||
As soon as we detect something wrong, we exit early. | ||
*/ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
👍
DFS 풀이보다 더 깔끔한 것 같아서 참고해보겠습니다!
target에서 빼면서 0인지 체크하는 풀이가 더 좋은 것 같네요!
(저는 더해서 체크하느라..)
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.
감사합니다! 이 정도 난이도는 처음 풀어봐서 많이 어려웠는데 백트래킹에 대해서 공부할 수 있는 시간이었습니다 ㅎㅎ 머지하겠습니다 !