-
-
Notifications
You must be signed in to change notification settings - Fork 304
[se6816] WEEK 03 Solutions #2103
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
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,40 @@ | ||
| /** | ||
| 중복 조합을 통해 조건에 맞는 조합을 찾는 방식 | ||
| candidates의 길이 -> N | ||
| 시간 복잡도 : O(2^N) | ||
| 공간 복잡도 : O(N) | ||
| */ | ||
| class Solution { | ||
| List<List<Integer>> list; | ||
| List<Integer> dataList; | ||
| public List<List<Integer>> combinationSum(int[] candidates, int target) { | ||
| Arrays.sort(candidates); | ||
| list = new ArrayList<>(); | ||
| dataList = new ArrayList<>(); | ||
| execute(candidates, 0, 0, target); | ||
| return list; | ||
| } | ||
|
|
||
| public void execute(int[] candidates, int idx, int sum, int target) { | ||
| if(idx >= candidates.length) { | ||
| return; | ||
| } | ||
|
|
||
| if(sum == target) { | ||
| list.add(new ArrayList<>(dataList)); | ||
| return; | ||
| } | ||
|
|
||
| if(sum+candidates[idx] > target) { | ||
| return; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| dataList.add(candidates[idx]); | ||
| execute(candidates, idx, sum + candidates[idx], target); | ||
| dataList.remove(dataList.size()-1); | ||
| execute(candidates, idx+1, sum, target); | ||
| } | ||
| } |
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,69 @@ | ||
| /** | ||
| HashSet과 substring을 이용하여 문자열을 비교하면서 가능한 조합의 수를 누적하는 방식 | ||
| 문자열 S 의 길이 -> N | ||
| 시간 복잡도 : O(N^2) | ||
| 공간 복잡도 : O(N) | ||
| */ | ||
| class Solution2 { | ||
| Set<String> numberSet = new HashSet<>(); | ||
| { | ||
| numberSet.addAll(Arrays.asList("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26")); | ||
| } | ||
| public int numDecodings(String s) { | ||
| if(s.length() == 1) return s.charAt(0) == '0' ? 0 : 1; | ||
|
|
||
| int[] list = new int[s.length()]; | ||
| list[0] = isImpossibleDecode(s.substring(0,1)) ? 0 : 1; | ||
| list[1] = isImpossibleDecode(s.substring(0,2)) ? 0 : 1; | ||
| list[1] += list[0]==1 && !isImpossibleDecode(s.substring(1,2)) ? 1 : 0; | ||
|
|
||
|
|
||
| for(int i = 2; i < s.length() ; i++) { | ||
| list[i] += isImpossibleDecode(s.substring(i-1,i+1)) ? 0 : list[i-2]; | ||
| list[i] += isImpossibleDecode(s.substring(i,i+1)) ? 0 : list[i-1]; | ||
| } | ||
|
|
||
| return list[s.length()-1]; | ||
|
|
||
| } | ||
|
|
||
| public boolean isImpossibleDecode(String target) { | ||
| int len = target.length(); | ||
| if(!numberSet.contains(target)) { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
|
|
||
| } | ||
| } | ||
|
|
||
| /** | ||
| 이전 substring() 연산 부분 최적화 | ||
| 이전 substring()으로 문자열을 자르지 않고, charAt()을 이용하여 직접 연산하여 조합의 수를 누적시키는 방식 | ||
| 문자열 S 의 길이 -> N | ||
| 시간 복잡도 : O(N) | ||
| 공간 복잡도 : O(N) | ||
| */ | ||
| class Solution { | ||
|
|
||
| public int numDecodings(String s) { | ||
| if(s.length() == 1) return s.charAt(0) == '0' ? 0 : 1; | ||
|
|
||
| int[] list = new int[s.length()]; | ||
| list[0] = s.charAt(0) == '0' ? 0 : 1; | ||
| int target = (s.charAt(0) - '0') * 10 + (s.charAt(1) - '0'); | ||
| list[1] = (target >= 10) && (target <= 26) ? 1 : 0; | ||
| list[1] += list[0]==1 && s.charAt(1) != '0' ? 1 : 0; | ||
|
|
||
|
|
||
| for(int i = 2; i < s.length() ; i++) { | ||
| target = (s.charAt(i-1) - '0') * 10 + (s.charAt(i) - '0'); | ||
| list[i] += (target >= 10) && (target <= 26) ? list[i-2] : 0; | ||
| list[i] += s.charAt(i) != '0' ? list[i-1] : 0; | ||
| } | ||
|
|
||
| return list[s.length()-1]; | ||
|
|
||
| } | ||
| } |
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,40 @@ | ||
| /** | ||
| dp를 이용하여 지속적으로 최대의 값을 기록하면서 연속 부분 배열의 최대 합을 구하는 방식 | ||
| nums 의 길이 -> N | ||
| 시간 복잡도 : O(N) | ||
| 공간 복잡도 : O(N) | ||
| */ | ||
| class Solution2 { | ||
| public int maxSubArray(int[] nums) { | ||
| int[] dp = new int[nums.length]; | ||
| dp[0] = nums[0]; | ||
| for(int i = 1; i < nums.length; i++) { | ||
| dp[i] = Math.max(nums[i], dp[i-1] + nums[i]); | ||
| } | ||
|
|
||
| for(int i = 0; i < dp.length; i++) { | ||
| } | ||
| return Arrays.stream(dp) | ||
| .max() | ||
| .getAsInt(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| 이전 방식과 동일하나 변수 하나로 이전 값만 유지하는 방식 | ||
| nums 의 길이 -> N | ||
| 시간 복잡도 : O(N) | ||
| 공간 복잡도 : O(1) | ||
| */ | ||
| class Solution { | ||
| public int maxSubArray(int[] nums) { | ||
| int prevSum = nums[0]; | ||
| int result = prevSum; | ||
| for(int i = 1; i < nums.length; i++) { | ||
| prevSum = Math.max(nums[i], prevSum + nums[i]); | ||
| result = Math.max(result, prevSum); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
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,30 @@ | ||
| /** | ||
| 비트 연산을 통해, 1의 개수를 구하는 방식 | ||
| 숫자 n -> N | ||
| 시간 복잡도 : O(logN) | ||
| 공간 복잡도 : O(1) | ||
| */ | ||
| class Solution2 { | ||
| public int hammingWeight(int n) { | ||
| int count = 0; | ||
| long bit = 1; | ||
| while(bit <= n) { | ||
| if((n & bit) > 0) { | ||
| count++; | ||
| } | ||
| bit <<= 1; | ||
| } | ||
| return count; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| Integer.bitCount() 메소드를 통해, 1의 개수를 구하는 방식 | ||
| 시간 복잡도 : O(1) | ||
| 공간 복잡도 : O(1) | ||
| */ | ||
| class Solution { | ||
| public int hammingWeight(int n) { | ||
| return Integer.bitCount(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. Java는 모르지만 bitCount라는 메소드 강력하네요! |
||
| } | ||
| } | ||
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 @@ | ||
| /** | ||
|
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. 투포인터로 풀이할 생각은 못했는데..한 수 배워갑니다.. |
||
| 투포인터 방식을 통해 맨 앞과 맨 끝 포인터에서 맨 끝 포인터가 맨 앞 포인터를 앞지를 때까지 비교하는 방식 | ||
| 문자열 s의 길이 -> N | ||
| 시간 복잡도 : O(N) | ||
| 공간 복잡도 : O(1) | ||
| */ | ||
| class Solution { | ||
| public boolean isPalindrome(String s) { | ||
| s=s.toUpperCase(); | ||
| int start=0; | ||
| int end=s.length()-1; | ||
| while(start < end){ | ||
| char ch=s.charAt(start); | ||
| char ch2=s.charAt(end); | ||
|
|
||
| // 문자 체크 | ||
| if(!((ch>=65 && ch<=90) || (ch>=48 && ch<=57))){ | ||
| start++; | ||
| continue; | ||
| } | ||
| if(!((ch2>=65 && ch2<=90) || (ch2>=48 && ch2<=57))){ | ||
| end--; | ||
| continue; | ||
| } | ||
|
|
||
| if(ch==ch2){ | ||
| start++; | ||
| end--; | ||
| continue; | ||
| } | ||
|
|
||
| return false; | ||
|
|
||
| } | ||
| 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를 사용해서 하는 방식도 좋은데 하나의 변수로 처리하는 것도 공간 복잡도를 개선시킬 수 있군요 👍