-
-
Notifications
You must be signed in to change notification settings - Fork 195
[권동현] Week 2 #740
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
[권동현] Week 2 #740
Changes from all commits
Commits
Show all changes
3 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,38 @@ | ||
/** | ||
* 특이사항 | ||
* 문제를 제대로 이해 못해서 풀이를 보며 이해했다. | ||
* 핵심은 중복 처리와 문제를 찾았을 때 종료가 아니라 다음 항목을 찾는 부분인 것 같다. | ||
* (추후 복습 예정) | ||
*/ | ||
|
||
class Solution { | ||
public List<List<Integer>> threeSum(int[] nums) { | ||
|
||
// (1) ArrayList | ||
// 시간복잡도 : O(N^2), 공간복잡도 : O(N) | ||
List<List<Integer>> result = new ArrayList<>(); | ||
Arrays.sort(nums); | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
if (i > 0 && nums[i] == nums[i - 1]) continue; | ||
|
||
int j = i + 1; | ||
int k = nums.length - 1; | ||
|
||
while (j < k) { | ||
int sumNum = nums[i] + nums[j] + nums[k]; | ||
|
||
if (sumNum > 0) k--; | ||
else if (sumNum < 0) j++; | ||
else { | ||
result.add(Arrays.asList(nums[i], nums[j], nums[k])); | ||
j++; | ||
|
||
while (nums[j] == nums[j - 1] && j < k) j++; | ||
} | ||
} | ||
} | ||
|
||
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,18 @@ | ||
class Solution { | ||
public int climbStairs(int n) { | ||
// dp 응용 버전 | ||
// 시간복잡도 : O(N), 공간복잡도 : O(1) | ||
|
||
int prev = 1, curr = 1; | ||
|
||
if (n == 1) return prev; | ||
|
||
for (int i = 2; i < n; i++) { | ||
int now = curr + prev; | ||
prev = curr; | ||
curr = now; | ||
} | ||
|
||
return curr; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
construct-binary-tree-from-preorder-and-inorder-traversal/kdh-92.java
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,36 @@ | ||
/** | ||
* 특이사항 | ||
* TreeNode 이해 & right 설정 부분 이해 필요 | ||
* - 재귀 호출 형태로 if 조건 추가 (preStart > preorder.length -1 || inStart > inEnd) 일 경우엔 null 반환 | ||
* - null일 때 상황 테스트하며 좀 더 살펴보기 | ||
* | ||
* - right | ||
* - preStart : preStart + inIdx - inStart + 1 (left 동일하게 preStart + 1에서 inIdx - inStart 를 추가해서 오른쪽 영역 탐색) | ||
*/ | ||
|
||
class Solution { | ||
public static TreeNode buildTree(int[] preorder, int[] inorder) { | ||
// (1) helper 메소드를 재귀호출 사용 | ||
// 시간복잡도 : O(N^2), 공간복잡도 : O(N) | ||
|
||
return helper(0, 0, inorder.length - 1, preorder, inorder); | ||
} | ||
|
||
public static TreeNode helper(int preStart, int inStart, int inEnd, int[] preorder, int[] inorder) { | ||
if (preStart > preorder.length - 1 || inStart > inEnd) return null; | ||
TreeNode node = new TreeNode(preorder[preStart]); | ||
int inIdx = 0; | ||
|
||
for (int i = inStart; i <= inEnd; i++) { | ||
if (node.val == inorder[i]) { | ||
inIdx = i; | ||
break; | ||
} | ||
} | ||
|
||
node.left = helper(preStart + 1, inStart, inIdx - 1, preorder, inorder); | ||
node.right = helper(preStart + inIdx - inStart + 1, inIdx + 1, inEnd, preorder, inorder); | ||
|
||
return node; | ||
} | ||
} |
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,66 @@ | ||
/** | ||
* 특이사항 | ||
* 시간복잡도가 (1)번 풀이는 O(N log N) / (2), (3)번 풀이는 O(N)인데 | ||
* 리트코드에서 Runtime - (1)번 : 3ms, (3)번 : 6ms, (2)번 : 13ms | ||
* | ||
* 시간복잡도로만 따지면 당연히 (2), (3)번 풀이가 빨라야하는데도 불구하고 오히려 (1)번 풀이가 더 빠른 결과가 나온다. | ||
* chatgpt 확인했을 때 가장 큰 차이점은 문자열을 배열로 변경해서 정렬을 하는데 이 때 정렬 알고리즘의 성능에 따라 차이가 난다는 내용이 있었다. | ||
* N이 높아질수록 (2), (3)의 성능이 좋아질 것으로 예상되나 해당 문제의 결과에서는 (1)의 성능이 좋게 나올 수 있다는 것을 알게 되었다. | ||
*/ | ||
|
||
class Solution { | ||
public boolean isAnagram(String s, String t) { | ||
// (1) 문자 배열 - 정렬 & 비교 | ||
// 시간복잡도 : O(N log N), 공간복잡도 : O(N) | ||
|
||
// char[] sArr = s.toCharArray(); | ||
// char[] tArr = t.toCharArray(); | ||
// Arrays.sort(sArr); | ||
// Arrays.sort(tArr); | ||
|
||
// return new String(sArr).equals(new String(tArr)); // 문자열을 비교한다 생각했을 때 방법 | ||
// return Arrays.equals(sArr, tArr); // char 배열 자체를 비교 | ||
|
||
// (2) HashMap 이용해 알파벳 개수 체크 | ||
// 시간복잡도 : O(N), 공간복잡도 : O(N) | ||
|
||
// Map<Character, Integer> count = new HashMap<>(); | ||
|
||
// for (char x : s.toCharArray()) { | ||
// count.put(x, count.getOrDefault(x, 0) + 1); | ||
// } | ||
|
||
// for (char x : t.toCharArray()) { | ||
// count.put(x, count.getOrDefault(x, 0) - 1); | ||
// } | ||
|
||
// for (int val : count.values()) { | ||
// if (val != 0) { | ||
// return false; | ||
// } | ||
// } | ||
|
||
// return true; | ||
|
||
// (3) 배열로 알파벳 개수 체크 | ||
// 시간복잡도 : O(N), 공간복잡도 : O(N) | ||
|
||
// if (s.length() != t.length()) { | ||
// return false; | ||
// } | ||
|
||
// int[] freq = new int[26]; | ||
// for (int i = 0; i < s.length(); i++) { | ||
// freq[s.charAt(i) - 'a']++; | ||
// freq[t.charAt(i) - 'a']--; | ||
// } | ||
|
||
// for (int i = 0; i < freq.length; i++) { | ||
// if (freq[i] != 0) { | ||
// return false; | ||
// } | ||
// } | ||
|
||
// return true; | ||
} | ||
} |
Oops, something went wrong.
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.
맞아요 :) 자바에서 기본적으로 제공하는 정렬 기능을 사용시 O(n log n)의 시간 복잡도가 소요되어 극한의 효율을 요구하는 문제에서는 종종 쓰기 힘든 경우가 있습니다.
하지만 이번 문제의 경우 정렬로 인한 시간 요소가 크게 영향을 미치지 않아서 괜찮을것 같아요!