Skip to content

Commit 70dd17b

Browse files
authored
Merge pull request #2017 from ymir0804/main
[ymir0804] WEEK 01 solutions
2 parents 7f70da2 + cc7207f commit 70dd17b

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

contains-duplicate/ymir0804.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.Arrays;
2+
import java.util.List;
3+
import java.util.stream.Collectors;
4+
5+
class Solution {
6+
public boolean containsDuplicate(int[] nums) {
7+
List<Integer> list = Arrays.stream(nums).boxed().collect(Collectors.toList());
8+
for (int i : nums) {
9+
int idx = list.indexOf(i);
10+
if (idx == -1) {
11+
continue;
12+
}
13+
list.remove(idx);
14+
if (list.contains(i)) {
15+
return true;
16+
}
17+
}
18+
return false;
19+
}
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.util.*;
2+
import java.util.stream.Collectors;
3+
4+
class Solution {
5+
public int[] topKFrequent(int[] nums, int k) {
6+
Set<Integer> unique = Arrays.stream(nums).boxed().collect(Collectors.toSet());
7+
Map<Integer, Integer> value = new HashMap<>();
8+
for (int n : unique) {
9+
int cnt = 0;
10+
for (int m : nums) {
11+
if (n == m) {
12+
cnt++;
13+
}
14+
}
15+
value.put(n, cnt);
16+
}
17+
List<Map.Entry<Integer, Integer>> sortedByValueDesc = value.entrySet()
18+
.stream()
19+
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
20+
.toList();
21+
return sortedByValueDesc.stream()
22+
.limit(k)
23+
.map(Map.Entry::getKey)
24+
.mapToInt(Integer::intValue)
25+
.toArray();
26+
}
27+
}

two-sum/ymir0804.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
class Solution {
5+
public int[] twoSum(int[] nums, int target) {
6+
// 시간 복잡도를 생각해서 HashMap을 사용
7+
8+
Map<Integer, Integer> idx = new HashMap<>();
9+
for (int i = 0; i < nums.length; i++) {
10+
int c = target - nums[i];
11+
if (idx.containsKey(c)) {
12+
return new int[]{idx.get(c), i};
13+
}
14+
idx.put(nums[i], i);
15+
}
16+
return new int[]{};
17+
}
18+
}

0 commit comments

Comments
 (0)