Skip to content

Commit cafbe96

Browse files
authored
Merge pull request #314 from f-exuan21/main
[์•„ํ˜„] Week01 Solutions
2 parents d19b3de + b6eefac commit cafbe96

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

โ€Žcontains-duplicate/f-exuan21.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// time : O(n)
2+
// space : O(n)
3+
4+
class Solution {
5+
public boolean containsDuplicate(int[] nums) {
6+
Set<Integer> set = new HashSet<>();
7+
for(int i : nums) {
8+
if(set.contains(i)) {
9+
return true;
10+
}
11+
set.add(i);
12+
}
13+
return false;
14+
}
15+
}
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
18+
private int count = 0;
19+
private TreeNode resultNode = null;
20+
21+
public int kthSmallest(TreeNode root, int k) {
22+
searchNode(root, k);
23+
return resultNode.val;
24+
}
25+
26+
public void searchNode(TreeNode node, int k) {
27+
28+
if(resultNode != null) return;
29+
30+
if(node.left != null) {
31+
searchNode(node.left, k);
32+
}
33+
34+
count++;
35+
36+
if(count == k) {
37+
resultNode = node;
38+
return;
39+
}
40+
41+
if(node.right != null) {
42+
searchNode(node.right, k);
43+
}
44+
}
45+
}
46+
47+
// n : ๋…ธ๋“œ ๊ฐœ์ˆ˜
48+
// time : O(n) ์ตœ์•…์˜ ๊ฒฝ์šฐ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•ด์•ผํ•จ
49+
// space : O(n) ์ตœ์•…์˜ ๊ฒฝ์šฐ ํ•œ ์ชฝ์œผ๋กœ ๋…ธ๋“œ๊ฐ€ ์น˜์šฐ์ณ์ ธ ์žˆ์Œ
50+
// -> ์žฌ๊ท€ ํ˜ธ์ถœ์ด ์ด๋ฃจ์–ด์ง€๋ฏ€๋กœ ์Šคํƒ์— ์Œ“์ž„ -> ํ•œ ์ชฝ์œผ๋กœ ์ ๋ ค ์žˆ์œผ๋ฉด ํŠธ๋ฆฌ์˜ ๋†’์ด๊ฐ€ n์ด ๋จ (ํŠธ๋ฆฌ์˜ ์ตœ๋Œ€ ๋†’์ด๊ฐ€ ์Šคํƒ์˜ ์ตœ๋Œ€ ๊นŠ์ด)

โ€Žnumber-of-1-bits/f-exuan21.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// time : O(1)
2+
// space : O(1)
3+
4+
class Solution {
5+
public int hammingWeight(int n) {
6+
int count = 0;
7+
8+
while(n != 0) {
9+
if((n&1) == 1) count++;
10+
n = n >> 1;
11+
}
12+
13+
return count;
14+
}
15+
}
16+
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
class Solution {
3+
public int[] topKFrequent(int[] nums, int k) {
4+
Map<Integer, Integer> map = new HashMap<>();
5+
for(int num : nums) {
6+
map.put(num, map.getOrDefault(num, 0) + 1);
7+
}
8+
9+
PriorityQueue<Map.Entry<Integer, Integer>> queue = new PriorityQueue<>(
10+
(a, b) -> Integer.compare(b.getValue(), a.getValue())
11+
);
12+
13+
for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
14+
queue.offer(entry);
15+
}
16+
17+
int[] res = new int[k];
18+
19+
for(int i = 0; i < k; i++) {
20+
res[i] = queue.poll().getKey();
21+
}
22+
23+
return res;
24+
}
25+
}
26+
27+
// n : nums์˜ ๊ธธ์ด
28+
// m : nums์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ์ˆซ์ž์˜ ๊ฐœ์ˆ˜
29+
30+
// time : O(n) + O(m*logm) + O(k*logm) = O(n + m*logm + k*logm)
31+
// ์ตœ์•…์˜ ๊ฒฝ์šฐ, nums ๊ฐ€ ๋‹ค unique ํ•˜๊ธฐ ๋•Œ๋ฌธ์— n == m == k ๊ฐ€ ๋จ
32+
// ๋”ฐ๋ผ์„œ, O(n*logn)
33+
34+
// space : O(m) + O(m) + O(k) = O(m + k)
35+
// ์ตœ์•…์˜ ๊ฒฝ์šฐ n == m == k ๊ฐ€ ๋จ
36+
// ๋”ฐ๋ผ์„œ, O(n)
37+
38+
39+

0 commit comments

Comments
ย (0)