-
-
Notifications
You must be signed in to change notification settings - Fork 305
[geegong] WEEK 01 Solutions #2018
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
2 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -1,4 +1,52 @@ | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class Geegong { | ||
| // 이 문제는 시간이 남을때 풀 예정 😅 | ||
|
|
||
| /** | ||
| * top-down + memoization 방식으로 풀이 | ||
| * memoization (memo 변수) 없이 풀이하면 Time Limit Exceeded 발생 | ||
| * time complexity : O(N) -> memo 가 있어서 이미 연산이 된건 패스함 | ||
| * space complexity : O(N) -> index 만큼의 연산 결과가 있음 | ||
| * @param nums | ||
| * @return | ||
| */ | ||
| public int rob(int[] nums) { | ||
| // memoization 하지 않으면 Time Limit Exceeded. | ||
| Map<Integer, Integer> memo = new HashMap<>(); | ||
|
|
||
| int maxAmount = 0; | ||
| for (int idx=0; idx<nums.length; idx++) { | ||
| int currAmount = Math.max( | ||
| nums[idx] + rob(nums, idx+2, memo), rob(nums, idx+1, memo)); | ||
| maxAmount = Math.max(currAmount, maxAmount); | ||
| } | ||
|
|
||
| return maxAmount; | ||
| } | ||
|
|
||
|
|
||
| public int rob(int[] origin, int currIdx, Map<Integer, Integer> memo) { | ||
| if (currIdx == origin.length - 1) { | ||
| return origin[currIdx]; | ||
| } else if (currIdx >= origin.length) { // when out of bounds | ||
| return 0; | ||
| } | ||
|
|
||
| if (memo.containsKey(currIdx)) { | ||
| return memo.get(currIdx); | ||
| } | ||
|
|
||
| int currentVal = origin[currIdx]; | ||
|
|
||
| int maxAmount = Math.max( | ||
| currentVal + rob(origin, currIdx + 2, memo), rob(origin, currIdx+1, memo)); | ||
|
|
||
| memo.put(currIdx, maxAmount); | ||
|
|
||
| return maxAmount; | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
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
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 |
|---|---|---|
| @@ -1,75 +1,128 @@ | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.*; | ||
|
|
||
| public class Geegong { | ||
|
|
||
|
|
||
| public int[] topKFrequent(int[] nums, int k) { | ||
| int[] result = new int[k]; | ||
| public class Geegong { | ||
|
|
||
| // key : num element in nums / value : frequency of num elements | ||
| Map<Integer, Integer> numMap = new HashMap<>(); | ||
|
|
||
| // key : frequency of num elements / value : HashSet<Integer> num elements | ||
| Map<Integer, HashSet<Integer>> frequencyMap = new HashMap<>(); | ||
| /** | ||
| * Map 으로 빈도수를 key , 빈도수에 해당되는 num 들을 list 로 저장 | ||
| * key (빈도수) 를 sorting | ||
| * k 만큼 골라낸다 | ||
| * Time Complexity : O(N) + O(N logN) + O(N) | ||
| * - O(N) : nums 만큼 iterate | ||
| * - O(N log N) : sorting | ||
| * - O(N) : frequency 그룹핑된 그룹 갯수만큼 iterate | ||
| * @param nums | ||
| * @param k | ||
| * @return int[] | ||
| */ | ||
| public int[] topKFrequent(int[] nums, int k) { | ||
|
|
||
| // most frequent numbers | ||
| int maxCount = 0; | ||
| // key : frequency , value : list of nums | ||
| Map<Integer, List<Integer>> map = new HashMap<>(); | ||
| Arrays.sort(nums); | ||
| int current = nums[0]; | ||
| int count = 1; | ||
|
|
||
| // initialize numMap | ||
| for (int num : nums) { | ||
| if (numMap.containsKey(num)) { | ||
| Integer alreadyCounted = numMap.get(num); | ||
| numMap.put(num, alreadyCounted + 1); | ||
| for (int i = 1; i < nums.length; i++) { | ||
| if (nums[i] == current) { | ||
| count++; | ||
| } else { | ||
| numMap.put(num, 1); | ||
| map.computeIfAbsent(count, el -> new ArrayList<>()).add(current); | ||
| current = nums[i]; | ||
| count = 1; | ||
| } | ||
| } | ||
|
|
||
| // Add last group | ||
| map.computeIfAbsent(count, el -> new ArrayList<>()).add(current); | ||
|
|
||
| //numHashSetMap | ||
| for (int num : numMap.keySet()) { | ||
| int frequencyOfNum = numMap.get(num); | ||
| maxCount = Math.max(maxCount, frequencyOfNum); | ||
|
|
||
| if (frequencyMap.containsKey(frequencyOfNum)) { | ||
| HashSet<Integer> alreadySet = frequencyMap.get(frequencyOfNum); | ||
| alreadySet.add(num); | ||
|
|
||
| frequencyMap.put(frequencyOfNum, alreadySet); | ||
|
|
||
| } else { | ||
| HashSet<Integer> newHashSet = new HashSet<>(); | ||
| newHashSet.add(num); | ||
|
|
||
| frequencyMap.put(frequencyOfNum, newHashSet); | ||
| } | ||
| } | ||
| List<Integer> sortedFrequency = map.keySet().stream().sorted(Comparator.reverseOrder()).toList(); | ||
| List<Integer> result = new ArrayList<>(); | ||
| for (int index=0; index<sortedFrequency.size(); index++ ) { | ||
| int mostFrequency = sortedFrequency.get(index); | ||
| List<Integer> numsOfFreq = map.get(mostFrequency); | ||
|
|
||
| for(int innerIndex = 0; innerIndex<numsOfFreq.size(); innerIndex++) { | ||
| if (result.size() == k) { | ||
| return result.stream().mapToInt(Integer::intValue).toArray(); | ||
| } | ||
|
|
||
| // maxCount 부터 decreasing | ||
| int resultIndex=0; | ||
| for(int frequency=maxCount; frequency>=0; frequency--) { | ||
| if (resultIndex >= result.length) { | ||
| return result; | ||
| result.add(numsOfFreq.get(innerIndex)); | ||
| } | ||
|
|
||
| if (frequencyMap.containsKey(frequency)) { | ||
| HashSet<Integer> numElements = frequencyMap.get(frequency); | ||
|
|
||
| for (int numElement : numElements) { | ||
| result[resultIndex] = numElement; | ||
| resultIndex++; | ||
|
|
||
|
|
||
| if (resultIndex >= result.length) { | ||
| return result; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| return result.stream().mapToInt(Integer::intValue).toArray(); | ||
|
|
||
| // 아래 문제풀이는 예전 기수에 풀었던 방법으로 Map 으로 빈도수와 num을 관리하는 값을 가지긴 하나 | ||
| // sorting은 하지 않고 maxNumOfFrequency를 구하여 순차적으로 작은 값들을 꺼내서 k만큼 리턴한다 | ||
| // int[] result = new int[k]; | ||
| // | ||
| // // key : num element in nums / value : frequency of num elements | ||
| // Map<Integer, Integer> numMap = new HashMap<>(); | ||
| // | ||
| // // key : frequency of num elements / value : HashSet<Integer> num elements | ||
| // Map<Integer, HashSet<Integer>> frequencyMap = new HashMap<>(); | ||
| // | ||
| // // most frequent numbers | ||
| // int maxCount = 0; | ||
| // | ||
| // // initialize numMap | ||
| // for (int num : nums) { | ||
| // if (numMap.containsKey(num)) { | ||
| // Integer alreadyCounted = numMap.get(num); | ||
| // numMap.put(num, alreadyCounted + 1); | ||
| // } else { | ||
| // numMap.put(num, 1); | ||
| // } | ||
| // } | ||
| // | ||
| // | ||
| // //numHashSetMap | ||
| // for (int num : numMap.keySet()) { | ||
| // int frequencyOfNum = numMap.get(num); | ||
| // maxCount = Math.max(maxCount, frequencyOfNum); | ||
| // | ||
| // if (frequencyMap.containsKey(frequencyOfNum)) { | ||
| // HashSet<Integer> alreadySet = frequencyMap.get(frequencyOfNum); | ||
| // alreadySet.add(num); | ||
| // | ||
| // frequencyMap.put(frequencyOfNum, alreadySet); | ||
| // | ||
| // } else { | ||
| // HashSet<Integer> newHashSet = new HashSet<>(); | ||
| // newHashSet.add(num); | ||
| // | ||
| // frequencyMap.put(frequencyOfNum, newHashSet); | ||
| // } | ||
| // } | ||
| // | ||
| // | ||
| // // maxCount 부터 decreasing | ||
| // int resultIndex=0; | ||
| // for(int frequency=maxCount; frequency>=0; frequency--) { | ||
| // if (resultIndex >= result.length) { | ||
| // return result; | ||
| // } | ||
| // | ||
| // if (frequencyMap.containsKey(frequency)) { | ||
| // HashSet<Integer> numElements = frequencyMap.get(frequency); | ||
| // | ||
| // for (int numElement : numElements) { | ||
| // result[resultIndex] = numElement; | ||
| // resultIndex++; | ||
| // | ||
| // | ||
| // if (resultIndex >= result.length) { | ||
| // return result; | ||
| // } | ||
| // } | ||
| // } | ||
| // } | ||
| // | ||
| // return result; | ||
| } | ||
| } | ||
|
|
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
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.
Uh oh!
There was an error while loading. Please reload this page.