Skip to content

Commit 37fa2b2

Browse files
committed
- 1주차 풀이 커밋
1 parent 941ab12 commit 37fa2b2

File tree

5 files changed

+280
-96
lines changed

5 files changed

+280
-96
lines changed

contains-duplicate/Geegong.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,23 @@ public class Geegong {
1212
* @return
1313
*/
1414
public boolean containsDuplicate(int[] nums) {
15+
16+
17+
1518
HashSet<Integer> uniques = new HashSet<>();
1619

1720
for (int num : nums) {
18-
if (uniques.contains(num)) {
21+
22+
// 명확하게 hashSet에 값이 있는지 체크하는 메소드로 확인이 가능하지만
23+
// if (uniques.contains(num)) {
24+
// return true;
25+
// }
26+
// uniques.add(num);
27+
28+
// hashSet 의 Add 는 이미 값이 있다면 FALSE를 리턴하기에 아래처럼도 동작 가능 (더 빠른 결과확인)
29+
if (!uniques.add(num)) {
1930
return true;
2031
}
21-
22-
uniques.add(num);
2332
}
2433

2534
return false;

house-robber/Geegong.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,52 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
14
public class Geegong {
2-
// 이 문제는 시간이 남을때 풀 예정 😅
5+
6+
/**
7+
* top-down + memoization 방식으로 풀이
8+
* memoization (memo 변수) 없이 풀이하면 Time Limit Exceeded 발생
9+
* time complexity : O(N) -> memo 가 있어서 이미 연산이 된건 패스함
10+
* space complexity : O(N) -> index 만큼의 연산 결과가 있음
11+
* @param nums
12+
* @return
13+
*/
14+
public int rob(int[] nums) {
15+
// memoization 하지 않으면 Time Limit Exceeded.
16+
Map<Integer, Integer> memo = new HashMap<>();
17+
18+
int maxAmount = 0;
19+
for (int idx=0; idx<nums.length; idx++) {
20+
int currAmount = Math.max(
21+
nums[idx] + rob(nums, idx+2, memo), rob(nums, idx+1, memo));
22+
maxAmount = Math.max(currAmount, maxAmount);
23+
}
24+
25+
return maxAmount;
26+
}
27+
28+
29+
public int rob(int[] origin, int currIdx, Map<Integer, Integer> memo) {
30+
if (currIdx == origin.length - 1) {
31+
return origin[currIdx];
32+
} else if (currIdx >= origin.length) { // when out of bounds
33+
return 0;
34+
}
35+
36+
if (memo.containsKey(currIdx)) {
37+
return memo.get(currIdx);
38+
}
39+
40+
int currentVal = origin[currIdx];
41+
42+
int maxAmount = Math.max(
43+
currentVal + rob(origin, currIdx + 2, memo), rob(origin, currIdx+1, memo));
44+
45+
memo.put(currIdx, maxAmount);
46+
47+
return maxAmount;
48+
}
49+
50+
351
}
452

Lines changed: 81 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import java.util.Arrays;
12
import java.util.HashMap;
23
import java.util.HashSet;
34
import java.util.Map;
@@ -7,43 +8,99 @@
78
*/
89
public class Geegong {
910

11+
/**
12+
* Time complexity : O(N) + O(N long N) + O(N)
13+
* - o(N) : 한번 순회해서 set
14+
* - O(N log N) : sorting
15+
* - O (N) : sorting된걸 한번 더 순회
16+
* Space complexity : O(N) -> hash set
17+
* @param nums
18+
* @return
19+
*/
1020
public int longestConsecutive(int[] nums) {
11-
HashSet<Integer> setOfNums = new HashSet<>();
12-
// key : startIndex , value : length
13-
Map<Integer, Integer> lengthMap = new HashMap<>();
14-
15-
// sort..? 를 해야될까 싶음..
21+
if (nums.length == 0) {
22+
return 0;
23+
}
1624

17-
// initialize
25+
// hashSet
26+
HashSet<Integer> hashSet = new HashSet<>();
1827
for (int num : nums) {
19-
setOfNums.add(num);
28+
hashSet.add(num);
2029
}
2130

22-
Integer longest = 0;
31+
int[] sortedNums = hashSet.stream().mapToInt(val -> val).sorted().toArray();
2332

24-
for (Integer num : setOfNums) {
25-
int length = iterate(setOfNums, num, 0, lengthMap);
26-
longest = Math.max(longest, length);
27-
}
33+
int maxLength = 1;
34+
int currentLength = 1;
35+
int prev = sortedNums[0];
36+
for(int index=1; index<sortedNums.length; index++) {
37+
int current = sortedNums[index];
38+
if (current == prev + 1) {
39+
currentLength++;
2840

29-
return longest;
30-
}
41+
} else {
42+
maxLength = Math.max(currentLength, maxLength);
43+
currentLength = 1;
44+
}
3145

32-
public Integer iterate(HashSet<Integer> hashSet, int currIndex, int currLength, Map<Integer, Integer> lengthMap) {
33-
if (lengthMap.containsKey(currIndex)) {
34-
return lengthMap.get(currIndex);
46+
prev = sortedNums[index];
3547
}
3648

37-
if (hashSet.contains(currIndex)) {
38-
currLength++;
39-
return iterate(hashSet, currIndex+1, currLength, lengthMap);
4049

41-
} else {
42-
lengthMap.put(currIndex, currLength);
43-
return currLength;
44-
}
50+
51+
return Math.max(currentLength, maxLength);
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
62+
63+
64+
65+
4566

4667
}
68+
// HashSet<Integer> setOfNums = new HashSet<>();
69+
// // key : startIndex , value : length
70+
// Map<Integer, Integer> lengthMap = new HashMap<>();
71+
//
72+
// // sort..? 를 해야될까 싶음..
73+
//
74+
// // initialize
75+
// for (int num : nums) {
76+
// setOfNums.add(num);
77+
// }
78+
//
79+
// Integer longest = 0;
80+
//
81+
// for (Integer num : setOfNums) {
82+
// int length = iterate(setOfNums, num, 0, lengthMap);
83+
// longest = Math.max(longest, length);
84+
// }
85+
//
86+
// return longest;
87+
// }
88+
//
89+
// public Integer iterate(HashSet<Integer> hashSet, int currIndex, int currLength, Map<Integer, Integer> lengthMap) {
90+
// if (lengthMap.containsKey(currIndex)) {
91+
// return lengthMap.get(currIndex);
92+
// }
93+
//
94+
// if (hashSet.contains(currIndex)) {
95+
// currLength++;
96+
// return iterate(hashSet, currIndex+1, currLength, lengthMap);
97+
//
98+
// } else {
99+
// lengthMap.put(currIndex, currLength);
100+
// return currLength;
101+
// }
102+
//
103+
// }
47104

48105
}
49106

Lines changed: 107 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,127 @@
1-
import java.util.HashMap;
2-
import java.util.HashSet;
3-
import java.util.Map;
1+
import java.util.*;
42

5-
public class Geegong {
63

74

8-
public int[] topKFrequent(int[] nums, int k) {
9-
int[] result = new int[k];
5+
public class Geegong {
106

11-
// key : num element in nums / value : frequency of num elements
12-
Map<Integer, Integer> numMap = new HashMap<>();
137

14-
// key : frequency of num elements / value : HashSet<Integer> num elements
15-
Map<Integer, HashSet<Integer>> frequencyMap = new HashMap<>();
8+
/**
9+
* Map 으로 빈도수를 key , 빈도수에 해당되는 num 들을 list 로 저장
10+
* key (빈도수) 를 sorting
11+
* k 만큼 골라낸다
12+
* Time Complexity : O(N) + O(N logN) + O(N)
13+
* - O(N) : nums 만큼 iterate
14+
* - O(N log N) : sorting
15+
* - O(N) : frequency 그룹핑된 그룹 갯수만큼 iterate
16+
* @param nums
17+
* @param k
18+
* @return
19+
*/
20+
public int[] topKFrequent(int[] nums, int k) {
1621

17-
// most frequent numbers
18-
int maxCount = 0;
22+
// key : frequency , value : list of nums
23+
Map<Integer, List<Integer>> map = new HashMap<>();
24+
Arrays.sort(nums);
25+
int current = nums[0];
26+
int count = 1;
1927

20-
// initialize numMap
21-
for (int num : nums) {
22-
if (numMap.containsKey(num)) {
23-
Integer alreadyCounted = numMap.get(num);
24-
numMap.put(num, alreadyCounted + 1);
28+
for (int i = 1; i < nums.length; i++) {
29+
if (nums[i] == current) {
30+
count++;
2531
} else {
26-
numMap.put(num, 1);
32+
map.computeIfAbsent(count, el -> new ArrayList<>()).add(current);
33+
current = nums[i];
34+
count = 1;
2735
}
2836
}
2937

38+
// Add last group
39+
map.computeIfAbsent(count, el -> new ArrayList<>()).add(current);
3040

31-
//numHashSetMap
32-
for (int num : numMap.keySet()) {
33-
int frequencyOfNum = numMap.get(num);
34-
maxCount = Math.max(maxCount, frequencyOfNum);
35-
36-
if (frequencyMap.containsKey(frequencyOfNum)) {
37-
HashSet<Integer> alreadySet = frequencyMap.get(frequencyOfNum);
38-
alreadySet.add(num);
39-
40-
frequencyMap.put(frequencyOfNum, alreadySet);
41-
42-
} else {
43-
HashSet<Integer> newHashSet = new HashSet<>();
44-
newHashSet.add(num);
45-
46-
frequencyMap.put(frequencyOfNum, newHashSet);
47-
}
48-
}
41+
List<Integer> sortedFrequency = map.keySet().stream().sorted(Comparator.reverseOrder()).toList();
42+
List<Integer> result = new ArrayList<>();
43+
for (int index=0; index<sortedFrequency.size(); index++ ) {
44+
int mostFrequency = sortedFrequency.get(index);
45+
List<Integer> numsOfFreq = map.get(mostFrequency);
4946

47+
for(int innerIndex = 0; innerIndex<numsOfFreq.size(); innerIndex++) {
48+
if (result.size() == k) {
49+
return result.stream().mapToInt(Integer::intValue).toArray();
50+
}
5051

51-
// maxCount 부터 decreasing
52-
int resultIndex=0;
53-
for(int frequency=maxCount; frequency>=0; frequency--) {
54-
if (resultIndex >= result.length) {
55-
return result;
52+
result.add(numsOfFreq.get(innerIndex));
5653
}
5754

58-
if (frequencyMap.containsKey(frequency)) {
59-
HashSet<Integer> numElements = frequencyMap.get(frequency);
60-
61-
for (int numElement : numElements) {
62-
result[resultIndex] = numElement;
63-
resultIndex++;
64-
65-
66-
if (resultIndex >= result.length) {
67-
return result;
68-
}
69-
}
70-
}
7155
}
7256

73-
return result;
57+
return result.stream().mapToInt(Integer::intValue).toArray();
58+
59+
60+
// int[] result = new int[k];
61+
//
62+
// // key : num element in nums / value : frequency of num elements
63+
// Map<Integer, Integer> numMap = new HashMap<>();
64+
//
65+
// // key : frequency of num elements / value : HashSet<Integer> num elements
66+
// Map<Integer, HashSet<Integer>> frequencyMap = new HashMap<>();
67+
//
68+
// // most frequent numbers
69+
// int maxCount = 0;
70+
//
71+
// // initialize numMap
72+
// for (int num : nums) {
73+
// if (numMap.containsKey(num)) {
74+
// Integer alreadyCounted = numMap.get(num);
75+
// numMap.put(num, alreadyCounted + 1);
76+
// } else {
77+
// numMap.put(num, 1);
78+
// }
79+
// }
80+
//
81+
//
82+
// //numHashSetMap
83+
// for (int num : numMap.keySet()) {
84+
// int frequencyOfNum = numMap.get(num);
85+
// maxCount = Math.max(maxCount, frequencyOfNum);
86+
//
87+
// if (frequencyMap.containsKey(frequencyOfNum)) {
88+
// HashSet<Integer> alreadySet = frequencyMap.get(frequencyOfNum);
89+
// alreadySet.add(num);
90+
//
91+
// frequencyMap.put(frequencyOfNum, alreadySet);
92+
//
93+
// } else {
94+
// HashSet<Integer> newHashSet = new HashSet<>();
95+
// newHashSet.add(num);
96+
//
97+
// frequencyMap.put(frequencyOfNum, newHashSet);
98+
// }
99+
// }
100+
//
101+
//
102+
// // maxCount 부터 decreasing
103+
// int resultIndex=0;
104+
// for(int frequency=maxCount; frequency>=0; frequency--) {
105+
// if (resultIndex >= result.length) {
106+
// return result;
107+
// }
108+
//
109+
// if (frequencyMap.containsKey(frequency)) {
110+
// HashSet<Integer> numElements = frequencyMap.get(frequency);
111+
//
112+
// for (int numElement : numElements) {
113+
// result[resultIndex] = numElement;
114+
// resultIndex++;
115+
//
116+
//
117+
// if (resultIndex >= result.length) {
118+
// return result;
119+
// }
120+
// }
121+
// }
122+
// }
123+
//
124+
// return result;
125+
}
74126
}
75127

0 commit comments

Comments
 (0)