-
-
Notifications
You must be signed in to change notification settings - Fork 202
[아현] Week01 Solutions #314
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
8 commits
Select commit
Hold shift + click to select a range
1c880ab
contains-duplicate solution
f-exuan21 b21432d
commit
5a960f8
Create f-exuan21.java
f-exuan21 dbbe7b3
Create f-exuan21.java
f-exuan21 a069025
Update f-exuan21.java
f-exuan21 cd94609
Update f-exuan21.java
f-exuan21 f89d7e1
Create f-exuan21.java
f-exuan21 b6eefac
Update f-exuan21.java
f-exuan21 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,16 @@ | ||
// time : O(n) | ||
// space : O(n) | ||
|
||
class Solution { | ||
public boolean containsDuplicate(int[] nums) { | ||
Set<Integer> set = new HashSet<>(); | ||
for(int i : nums) { | ||
if(set.contains(i)) { | ||
return true; | ||
} | ||
set.add(i); | ||
} | ||
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,50 @@ | ||
/** | ||
* 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 { | ||
|
||
private int count = 0; | ||
private TreeNode resultNode = null; | ||
|
||
public int kthSmallest(TreeNode root, int k) { | ||
searchNode(root, k); | ||
return resultNode.val; | ||
} | ||
|
||
public void searchNode(TreeNode node, int k) { | ||
|
||
if(resultNode != null) return; | ||
|
||
if(node.left != null) { | ||
searchNode(node.left, k); | ||
} | ||
|
||
count++; | ||
|
||
if(count == k) { | ||
resultNode = node; | ||
return; | ||
} | ||
|
||
if(node.right != null) { | ||
searchNode(node.right, k); | ||
} | ||
} | ||
} | ||
|
||
// n : 노드 개수 | ||
// time : O(n) 최악의 경우 모든 노드를 탐색해야함 | ||
// space : O(n) 최악의 경우 한 쪽으로 노드가 치우쳐져 있음 | ||
// -> 재귀 호출이 이루어지므로 스택에 쌓임 -> 한 쪽으로 쏠려 있으면 트리의 높이가 n이 됨 (트리의 최대 높이가 스택의 최대 깊이) |
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,16 @@ | ||
// time : O(1) | ||
// space : O(1) | ||
|
||
class Solution { | ||
public int hammingWeight(int n) { | ||
int count = 0; | ||
|
||
while(n != 0) { | ||
if((n&1) == 1) count++; | ||
n = 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,39 @@ | ||
|
||
class Solution { | ||
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); | ||
} | ||
|
||
PriorityQueue<Map.Entry<Integer, Integer>> queue = new PriorityQueue<>( | ||
(a, b) -> Integer.compare(b.getValue(), a.getValue()) | ||
); | ||
Comment on lines
+9
to
+11
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. 저는 Map.Entry를 ArrayList를 사용하고 정렬을 했는데, PriorityQueue 사용하니까
|
||
|
||
for(Map.Entry<Integer, Integer> entry : map.entrySet()) { | ||
queue.offer(entry); | ||
} | ||
|
||
int[] res = new int[k]; | ||
|
||
for(int i = 0; i < k; i++) { | ||
res[i] = queue.poll().getKey(); | ||
} | ||
|
||
return res; | ||
} | ||
} | ||
|
||
// n : nums의 길이 | ||
// m : nums에서 서로 다른 숫자의 개수 | ||
|
||
// time : O(n) + O(m*logm) + O(k*logm) = O(n + m*logm + k*logm) | ||
// 최악의 경우, nums 가 다 unique 하기 때문에 n == m == k 가 됨 | ||
// 따라서, O(n*logn) | ||
|
||
// space : O(m) + O(m) + O(k) = O(m + k) | ||
// 최악의 경우 n == m == k 가 됨 | ||
// 따라서, O(n) | ||
|
||
|
||
|
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.
(그냥 의견)
merge 함수를 써보시는것도 좋을 수 있을 것 같습니다.
e.g.