-
-
Notifications
You must be signed in to change notification settings - Fork 245
[윤태권] Week1 문제 풀이 #317
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
[윤태권] Week1 문제 풀이 #317
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bacf40e
contains-duplicate
taekwon-dev c75f0f1
refactor: use Set interface instead of HashSet for variable declaration
taekwon-dev 003b675
docs: add space complexity explanation
taekwon-dev fbc65ac
top-k-frequent-elements
taekwon-dev a1ff55e
number-of-1-bits
taekwon-dev 1be1c5b
kth-smallest-element-in-a-bst
taekwon-dev ca2017a
chore: add final newline
taekwon-dev 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,22 @@ | ||
class Solution { | ||
/** | ||
* 시간 복잡도: O(N) | ||
* - 최악의 경우 주어진 배열의 마지막 원소까지 체크해야 함. | ||
* - 중복 검사를 위해 Set 선택한 이유 | ||
* - O(1) 으로 탐색 가능 | ||
* - 데이터 순서 보장 필요 없음 | ||
* | ||
* 공간 복잡도: O(N) | ||
* - 최악의 경우 주어진 배열의 모든 원소가 자료구조에 저장되므로 O(N) | ||
*/ | ||
public boolean containsDuplicate(int[] nums) { | ||
Set<Integer> set = new HashSet<>(); | ||
for (int num: nums) { | ||
if (set.contains(num)) { | ||
return true; | ||
} | ||
set.add(num); | ||
} | ||
return false; | ||
} | ||
} |
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 @@ | ||
/** | ||
* 사고의 흐름: | ||
* - Binary Search Tree & 순서? | ||
* - In-order 방식으로 순회를 해야겠다. | ||
* - 1-indexed 이므로 리스트에 옮기고 k번째 접근한다면 -1 해서 접근해야겠다. | ||
* - 근데, In-order 순회 어떻게 하더라? ㅋㅋ (재귀로 했던 것 같은데..) >> 여기서 검색 (...) | ||
* | ||
* 시간 복잡도: O(N) | ||
* - BST 모든 노드를 다 순회해야 함 | ||
* | ||
* 공간 복잡도: O(N) | ||
* - 내가 활용한 스택을 기준으로 보면, | ||
* - Stack 에 최대로 많이 들어갈 수 있는 수는 BST 높이 | ||
* - BST 는 최악의 경우 한 쪽으로 완전히 편향되어 높이가 N이 될 수도 있고, | ||
* - 균형이 잘 맞다면 (마치 AVL, Red-Black) log N이 될 수 있음 | ||
* - 따라서 O(N) 이지만, 트리의 균형이 잘 맞다면 O(log N) | ||
*/ | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
class Solution { | ||
public int kthSmallest(TreeNode root, int k) { | ||
Stack<TreeNode> stack = new Stack<>(); | ||
TreeNode current = root; | ||
int count = 0; | ||
|
||
while (current != null || !stack.isEmpty()) { | ||
while (current != null) { | ||
stack.push(current); | ||
current = current.left; | ||
} | ||
|
||
current = stack.pop(); | ||
count++; | ||
|
||
if (count == k) { | ||
return current.val; | ||
} | ||
|
||
current = current.right; | ||
} | ||
|
||
return -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,22 @@ | ||
class Solution { | ||
/** | ||
* 시간 복잡도: O(log N) | ||
* - 주어진 수를 2로 나누는 행위를 반복 (주어진 수가 0이 될 때까지) | ||
* - 이는 반대로 보면 2를 몇 번 곱해야 N이 나오는지를 확인하는 과정과 같음 (로그의 의미..ㅎ) | ||
* - 또 생각해보니까 Binary Search Tree 에서 특정 값을 조회할 때와 같은 흐름이었던 것 같음! | ||
* | ||
* 공간 복잡도: O(1) | ||
* - 자료구조를 활용하지 않음 | ||
*/ | ||
public int hammingWeight(int n) { | ||
int result = 0; | ||
while (n >= 1) { | ||
if ((n % 2) == 1) { | ||
result++; | ||
} | ||
n /= 2; | ||
} | ||
|
||
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,37 @@ | ||
class Solution { | ||
/** | ||
* 사고의 흐름: | ||
* - Map 이용해서 각 키 값 별로 몇 개가 있는지 저장하자. | ||
* - 일단 주어진 모든 배열을 확인해야 한다. | ||
* - 정렬이 필요하겠다. | ||
* - Map의 값을 기준으로 정렬을 하는데, 키 값도 같이 따라와야겠네? | ||
* - Comparable? Comparator? (하 ... 이거 어떻게 했더라..) | ||
* | ||
* 시간 복잡도: O(NlogN) | ||
* - ArrayList sort() 는 Merge Sort | ||
* - Merge Sort 는 O(NlogN) | ||
* | ||
* 공간 복잡도: O(N) | ||
* - 최악의 경우 주어진 배열의 모든 원소가 자료구조에 저장되므로 O(N) | ||
*/ | ||
public int[] topKFrequent(int[] nums, int k) { | ||
Map<Integer, Integer> map = new HashMap<>(); | ||
for (int num: nums) { | ||
map.put(num, map.getOrDefault(num, 0) + 1); | ||
} | ||
List<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet()); | ||
list.sort(new Comparator<Map.Entry<Integer, Integer>>() { | ||
@Override | ||
public int compare(Map.Entry<Integer, Integer> e1, Map.Entry<Integer, Integer> e2) { | ||
return e2.getValue().compareTo(e1.getValue()); | ||
} | ||
}); | ||
Comment on lines
+22
to
+28
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. 익명 클래스를 람다식으로 바꿔 보시는것도 좋을것 같아요 :) |
||
|
||
int[] result = new int[k]; | ||
for (int i = 0; i < k; i++) { | ||
result[i] = list.get(i).getKey(); | ||
} | ||
|
||
return result; | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.