-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Yn3-3xh] WEEK 03 solutions #1293
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,37 @@ | ||
/** | ||
[문제풀이] | ||
- target 숫자를 주어진 배열로 나눠보면서 0이 되는 것들을 고르면 되지 않을까? | ||
- dfs로 풀어보자. | ||
time: O(2^N), space: O(N); | ||
|
||
[회고] | ||
!!! dfs에서 기존 리스트가 이후에 변경될 수 있으므로 새 객체를 생성해서 넣어줘야 한다. !!! | ||
|
||
DP로도 풀어보려고 했지만, 솔루션을 봐도 내가 풀 수 없을 것 같았다.. | ||
*/ | ||
class Solution { | ||
public List<List<Integer>> combinationSum(int[] candidates, int target) { | ||
List<List<Integer>> results = new ArrayList<>(); | ||
dfs(candidates, target, 0, results, new ArrayList<>(), 0); | ||
return results; | ||
} | ||
|
||
private void dfs(int[] candidates, int target, int index, List<List<Integer>> results, List<Integer> result, int sum) { | ||
if (sum == target) { | ||
results.add(new ArrayList<>(result)); | ||
return; | ||
} | ||
|
||
if (sum > target || index >= candidates.length) { | ||
return; | ||
} | ||
|
||
int num = candidates[index]; | ||
result.add(num); | ||
dfs(candidates, target, index, results, result, sum + num); | ||
|
||
result.remove(result.size() - 1); | ||
dfs(candidates, target, index + 1, results, result, sum); | ||
} | ||
} | ||
|
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,58 @@ | ||
/** | ||
[문제풀이] | ||
- 만들 수 있는 가지수를 모두 구하기 | ||
- A ~ Z -> 1 ~ 26 | ||
- DP로 풀어보자. | ||
time: O(N), space: O(N) | ||
1212를 예시로, | ||
첫번째 수를 가져올 때 dp[1] = 1 | ||
만들어지는 수: 1 | ||
|
||
한자리 수를 가져올 때 dp[2] = dp[1] = 1 | ||
만들어지는 수: 1, 2 | ||
두자리 수를 가져올 때 dp[2] = d[2] + dp[0] = 1 + 1 = 2 | ||
만들어지는 수: 12 | ||
>> 1, 2 | 12 | ||
|
||
한자리 수를 가져올 때 dp[3] = dp[2] = 2 | ||
만들어지는 수: 1, 2, 1 | 12, 1 | ||
두자리 수를 가져올 때 dp[3] = dp[3] + dp[1] = 2 + 1 = 3 | ||
만들어지는 수: 1, 21 | ||
>> 1, 2, 1 | 12, 1 | 1, 21 | ||
|
||
한자리 수를 가져올 때 dp[4] = dp[3] = 3 | ||
만들어지는 수: 1, 2, 1, 2 | 12, 1, 2 | 1, 21, 2 | ||
두자리 수를 가져올 때 dp[4] = dp[4] + dp[2] = 3 + 2 = 5 | ||
만들어지는 수: 1, 2, 12 | 12, 2 | ||
>> 1, 2, 1, 2 | 12, 1, 2 | 1, 21, 2 | 1, 2, 12 | 12, 2 | ||
|
||
[회고] | ||
dfs로 풀려다가 잘 안풀어져서 DP로 노선을 틀었는데, 아직 DP 구현이 모자른 것 같다.. | ||
|
||
*/ | ||
class Solution { | ||
public int numDecodings(String s) { | ||
if (s.charAt(0) == '0') { | ||
return 0; | ||
} | ||
|
||
int len = s.length(); | ||
int[] dp = new int[len + 1]; | ||
dp[0] = 1; | ||
dp[1] = 1; | ||
|
||
for (int i = 2; i <= len; i++) { | ||
int one = Character.getNumericValue(s.charAt(i - 1)); | ||
int two = Integer.parseInt(s.substring(i - 2, i)); | ||
|
||
if (1 <= one && one <= 9) { | ||
dp[i] = dp[i - 1]; | ||
} | ||
if (10 <= two && two <= 26) { | ||
dp[i] += dp[i - 2]; | ||
} | ||
} | ||
return dp[len]; | ||
} | ||
} | ||
|
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,23 @@ | ||
/** | ||
[문제풀이] | ||
- 주어진 배열에서 연속된 수 배열의 합이 큰 수 구하기 | ||
- (1) 현재수와 현재까지의 합 중 큰 수 구하기 : 현재 인덱스부터 시작되거나, 현재 인덱스까지 더해질 것 | ||
- (2) 최댓값과 (1)번 수 중 큰 수 구하기 | ||
time: O(N), space: O(1) | ||
|
||
[회고] | ||
솔루션까지는 근접하는데, 결국 해결은 자꾸 안된다.. | ||
어떻게 접근해야 솔루션까지 도달 할 수 있을까.. | ||
*/ | ||
class Solution { | ||
public int maxSubArray(int[] nums) { | ||
int max = nums[0]; | ||
int sum = nums[0]; | ||
for (int i = 1; i < nums.length; i++) { | ||
sum = Math.max(nums[i], sum + nums[i]); | ||
max = Math.max(max, sum); | ||
} | ||
return max; | ||
} | ||
} | ||
|
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,39 @@ | ||
/** | ||
[문제풀이] | ||
- 주어진 양의 정수를 2진수로 변환하여, 1의 개수 구하기 | ||
|
||
- 풀이 1 | ||
time: O(log N), space: O(1) | ||
class Solution { | ||
public int hammingWeight(int n) { | ||
int count = 0; | ||
while (n > 0) { | ||
if (n % 2 != 0) { | ||
count++; | ||
} | ||
n /= 2; | ||
} | ||
return count; | ||
} | ||
} | ||
- 풀이 2 | ||
time: O(log N), space: O(1) | ||
|
||
[회고] | ||
`n >> 1`비트 연산자는 `n / 2` 와 같다. | ||
`n >> 1`로 풀면 비교도 비트로! | ||
|
||
이진 표현에서 1의 개수를 세어주는 Integer.bitCount() 메서드도 있었다. | ||
*/ | ||
class Solution { | ||
public int hammingWeight(int n) { | ||
int count = 0; | ||
while (n > 0) { | ||
if ((n & 1) == 1) { | ||
count++; | ||
} | ||
n >>= 1; | ||
} | ||
return count; | ||
} | ||
} |
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,73 @@ | ||
/** | ||
[문제풀이] | ||
- 대문자 -> 소문자 변환 | ||
- 소문자가 아닌 문자면 넘어가고, 소문자만 체크 | ||
- 숫자는 넘어갈 수 없다. | ||
- 소문자 input, reverse input 비교 | ||
|
||
- 풀이1 | ||
time: O(N), space: O(1) | ||
class Solution { | ||
public boolean isPalindrome(String s) { | ||
if (s.trim().length() == 0) { | ||
return true; | ||
} | ||
|
||
s = s.toLowerCase(); | ||
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--; | ||
} | ||
|
||
if (s.charAt(left) != s.charAt(right)) { | ||
return false; | ||
} | ||
left++; | ||
right--; | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
- 풀이2 | ||
time: O(N), space: O(1) | ||
|
||
[회고] | ||
중간에 공백을 replace하면 안좋을 것 같은데.. | ||
-> 알파벳인지를 비교해서 인덱스를 바꾸자! | ||
|
||
for문만 고집하지 말고, while문도 사용해보자! | ||
(처음에 for문으로 풀다가 스파게티가 되어버렸ㄷ..) | ||
|
||
!!! String.toLowerCase() 로 소문자로 변환하는 것이 좋은 방법일까?? !!! | ||
*/ | ||
|
||
class Solution { | ||
public boolean isPalindrome(String s) { | ||
s = s.toLowerCase(); | ||
int left = 0; | ||
int right = s.length() - 1; | ||
while (left < right) { | ||
if (!Character.isLetterOrDigit(s.charAt(left))) { | ||
left++; | ||
continue; | ||
} | ||
if (!Character.isLetterOrDigit(s.charAt(right))) { | ||
right--; | ||
continue; | ||
} | ||
|
||
if (s.charAt(left) != s.charAt(right)) { | ||
return false; | ||
} | ||
left++; | ||
right--; | ||
} | ||
return true; | ||
} | ||
} |
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.
고민고민하다가 결국 못풀었던 문제인데 dp로 풀수 있었군요. 이번주도 수고하셨습니다!