Skip to content

Commit 7ef534d

Browse files
authored
Merge pull request #1726 from suhyenim/5th/week01
[suhyenim] WEEK01 solutions
2 parents 3883fe4 + b8a21e0 commit 7ef534d

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed

contains-duplicate/suhyenim.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* [5th/week01] 217. Contains Duplicate
2+
3+
1. 문제 요약
4+
링크: https://leetcode.com/problems/contains-duplicate/
5+
배열에서 동일한 숫자가 2번 이상 존재하면 true 반환, 아니면 false 반환
6+
7+
2. 풀이 로직
8+
제출1: 반복문을 중첩으로 돌리면서, 동일한 값을 발견하면 바로 true 반환
9+
실패: Time Limit Exceeded 발생
10+
=> 시간 복잡도: O(n^2), 공간 복잡도: O(1)
11+
class Solution {
12+
public boolean containsDuplicate(int[] nums) {
13+
for (int i = 0; i < nums.length; i++) {
14+
for (int j = i + 1; j < nums.length; j++) {
15+
if (nums[i] == nums[j]) {
16+
return true;
17+
}
18+
}
19+
}
20+
return false;
21+
}
22+
}
23+
24+
풀이2: 크기를 오름차순으로 정렬해서, 바로 옆 숫자랑 겹치는지 확인
25+
성공: Time: 21 ms (19.91%), Space: 56.4 MB (98.12%)
26+
=> 시간 복잡도: O(nlog(n)), 공간 복잡도: O(1)
27+
class Solution {
28+
public boolean containsDuplicate(int[] nums) {
29+
Arrays.sort(nums);
30+
for (int i = 0; i < nums.length - 1; i++)
31+
if (nums[i] == nums[i + 1]) return true;
32+
return false;
33+
}
34+
}
35+
36+
풀이3: 배열을 순회하면서 HashSet에 겹치는 숫자가 존재하면 true 반환, 아니라면 HashSet에 본(seen) 숫자로 추가
37+
성공: Time: 14 ms (61.51%), Space: 58.2 MB (61.9%)
38+
=> 시간 복잡도: O(n), 공간 복잡도: O(n)
39+
class Solution {
40+
public boolean containsDuplicate(int[] nums) {
41+
HashSet<Integer> seen = new HashSet<>();
42+
for (int i = 0; i < nums.length; i++) {
43+
if (seen.contains(nums[i])) return true;
44+
seen.add(nums[i]);
45+
}
46+
return false;
47+
}
48+
}
49+
50+
3. TIL
51+
그리고 메모리가 충분하고 빠른 속도가 필요할 때, HashSet을 사용해볼 수 있다.
52+
하지만 메모리가 여유롭지 않다면, HashSet을 사용하는 방식보다는 기존의 정렬을 사용하는 방식이 알맞을 수 있다.
53+
54+
*/
55+
56+
import java.util.Arrays;
57+
58+
class Solution {
59+
public boolean containsDuplicate(int[] nums) {
60+
Arrays.sort(nums);
61+
for (int i = 0; i < nums.length - 1; i++)
62+
if (nums[i] == nums[i + 1]) return true;
63+
return false;
64+
}
65+
}

top-k-frequent-elements/suhyenim.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* [5th/week01] 347. Top K Frequent Elements
2+
3+
1. 문제 요약
4+
링크: https://leetcode.com/problems/top-k-frequent-elements/description/
5+
가장 빈도가 높은 k개의 숫자를 배열로 반환
6+
7+
2. 풀이 로직
8+
풀이1: 빈도 HashMap을 만들고 -> 높은 빈도 순으로 key 정렬 -> top k까지만 배열로 반환
9+
성공: Time: 17 ms (12.72%), Space: 48.9 MB (34.62%)
10+
=> 시간 복잡도: O(nlogn), 공간 복잡도: O(n)
11+
12+
class Solution {
13+
public int[] topKFrequent(int[] nums, int k) {
14+
Map<Integer, Integer> count = new HashMap<>();
15+
for(int n : nums) {
16+
if(!count.containsKey(n)) {
17+
count.put(n, 1);
18+
} else {
19+
count.put(n, count.get(n) + 1);
20+
}
21+
}
22+
23+
List<Integer> keys = new ArrayList<>(count.keySet());
24+
keys.sort((a, b) -> count.get(b) - count.get(a));
25+
26+
int[] answer = new int[k];
27+
for (int i = 0; i < k; i++){
28+
answer[i] = keys.get(i);
29+
}
30+
31+
return answer;
32+
}
33+
}
34+
35+
3. TIL
36+
value값 기준으로 key를 내림차순 정렬하는 방법: keys.sort((a, b) -> count.get(b) - count.get(a));
37+
38+
*/
39+
40+
class Solution {
41+
public int[] topKFrequent(int[] nums, int k) {
42+
Map<Integer, Integer> count = new HashMap<>();
43+
for(int n : nums) {
44+
if(!count.containsKey(n)) {
45+
count.put(n, 1);
46+
} else {
47+
count.put(n, count.get(n) + 1);
48+
}
49+
}
50+
51+
List<Integer> keys = new ArrayList<>(count.keySet());
52+
keys.sort((a, b) -> count.get(b) - count.get(a));
53+
54+
int[] answer = new int[k];
55+
for (int i = 0; i < k; i++){
56+
answer[i] = keys.get(i);
57+
}
58+
59+
return answer;
60+
}
61+
}

two-sum/suhyenim.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* [5th/week01] 1. Two Sum
2+
3+
1. 문제 요약
4+
링크: https://leetcode.com/problems/two-sum/description/
5+
배열 내 두 숫자의 합이 target이 되는 key들을 배열로 반환
6+
(특이점: 답은 반드시 하나만 존재)
7+
8+
2. 풀이 로직
9+
제출1: 반복문을 중첩으로 돌리면서, 두 숫자의 합이 target일 때의 두 인덱스를 배열로 반환
10+
성공: Time: 45 ms (30.61%), Space: 44.9 MB (63.79%)
11+
=> 시간 복잡도: O(n^2), 공간 복잡도: O(1)
12+
class Solution {
13+
public int[] twoSum(int[] nums, int target) {
14+
for (int i = 0; i < nums.length - 1; i++) {
15+
for (int j = i + 1; j < nums.length; j++) {
16+
if (nums[i] + nums[j] == target)
17+
return new int[]{i, j};
18+
}
19+
}
20+
return null;
21+
}
22+
}
23+
24+
풀이2: target과의 차가 HashMap의 key들 중에 존재하면 두 인덱스를 배열로 반환, 아니라면 HashMap에 value-key쌍으로 추가
25+
성공: 2 ms (98.95%), Space: 45.1 MB (30.45%)
26+
=> 시간 복잡도: O(n), 공간 복잡도: O(n)
27+
class Solution {
28+
public int[] twoSum(int[] nums, int target) {
29+
Map<Integer, Integer> indices = new HashMap<>();
30+
for (int i = 0; i < nums.length; i++) {
31+
int complement = target - nums[i];
32+
if (indices.containsKey(complement)) {
33+
int j = indices.get(complement);
34+
return new int[]{j, i};
35+
}
36+
indices.put(nums[i], i);
37+
}
38+
return null;
39+
}
40+
}
41+
42+
3. TIL
43+
HashTable을 활용해서 key-value 자료구조를 구현할 수 있다.
44+
45+
*/
46+
47+
class Solution {
48+
public int[] twoSum(int[] nums, int target) {
49+
for (int i = 0; i < nums.length - 1; i++) {
50+
for (int j = i + 1; j < nums.length; j++) {
51+
if (nums[i] + nums[j] == target)
52+
return new int[]{i, j};
53+
}
54+
}
55+
return null;
56+
}
57+
}

0 commit comments

Comments
 (0)