-
-
Notifications
You must be signed in to change notification settings - Fork 319
[dohyeon2] WEEK 1 Solutions #2349
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
base: main
Are you sure you want to change the base?
Changes from all commits
8ae6dff
1041629
4388995
3187987
b613632
659e802
c86b201
b3dc2a0
aa308f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import java.util.HashSet; | ||
|
|
||
| class Solution { | ||
| // The problem is to check if there are any duplicate elements in the array. | ||
| // So, I decided to use HashSet because it has O(1) time complexity for add and contains operations which are good to use for checking duplicates. | ||
| public boolean containsDuplicate(int[] nums) { | ||
| // the element type of the array is int, so create Integer HashSet | ||
| // O(n) space complexity | ||
| HashSet<Integer> exists = new HashSet<Integer>(); | ||
| // the nums array has length up to 10^5, so use int type | ||
| // O(n) time complexity average | ||
| // worst case: O(n log n) if many elements hash to the same bucket | ||
| for(int i = 0; i < nums.length; i++){ | ||
| int num = nums[i]; | ||
|
|
||
| if(exists.contains(num)){ | ||
| return true; | ||
| }else{ | ||
| exists.add(num); | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| class Solution { | ||
| public int rob(int[] nums) { | ||
| // Dynamic Planning | ||
| // Time Complexity: O(n) | ||
| // Space Complexity: O(n) for the dp array | ||
| // When applying DP, it is important to define the accumulated state clearly. | ||
|
|
||
| if (nums.length == 1) { | ||
| return nums[0]; | ||
| } | ||
|
|
||
| // The state of dp is maximum amount of robbery at i | ||
| int[] dp = new int[nums.length]; | ||
|
|
||
| dp[0] = nums[0]; | ||
| dp[1] = Math.max(nums[0], nums[1]); | ||
|
|
||
| for (int i = 2; i < nums.length; i++) { | ||
| dp[i] = Math.max( | ||
| // If we skip the current house, | ||
| // we can take the maximum amount of robbery at i - 1 | ||
| dp[i - 1], | ||
| // If we rob the current house, we cannot rob the previous house | ||
| // so we can take the maximum amount of robbery at i - 2 | ||
| dp[i - 2] + nums[i]); | ||
| } | ||
|
|
||
| return dp[nums.length - 1]; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import java.util.HashSet; | ||
|
|
||
| class Solution { | ||
| // This solution was inspired by: | ||
| // https://www.algodale.com/problems/longest-consecutive-sequence/ | ||
| // | ||
| // I initially believed this algorithm would run in O(n) time, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사고흐름을 상세하게 기술해놓는 것 정말 좋은 것 같습니다! |
||
| // but it resulted in a Time Limit Exceeded error. | ||
| // | ||
| // Although the expected time complexity is O(n), | ||
| // repeatedly calling set.iterator().next() might introduce overhead. | ||
| // Iterating through the set using a for-loop may be a better approach. | ||
| // | ||
| // In this case, it seems preferable to follow the approach described here: | ||
| // https://www.algodale.com/problems/longest-consecutive-sequence/#%ED%92%80%EC%9D%B4-3 | ||
|
|
||
| public int oldApproach(int[] nums) { | ||
| int count = 0; | ||
| HashSet<Integer> set = new HashSet<>(); | ||
| for (int num : nums) { | ||
| set.add(num); | ||
| } | ||
|
|
||
| while (set.size() > 0) { | ||
| int buffer = 1; | ||
| // This may cause a Time Limit Exceeded error. | ||
| Integer curr = set.iterator().next(); | ||
| set.remove(curr); | ||
| Integer next = curr + 1; | ||
| Integer prev = curr - 1; | ||
|
|
||
| while (set.contains(next)) { | ||
| set.remove(next); | ||
| next++; | ||
| buffer++; | ||
| } | ||
|
|
||
| while (set.contains(prev)) { | ||
| set.remove(prev); | ||
| prev--; | ||
| buffer++; | ||
| } | ||
|
|
||
| count = Math.max(count, buffer); | ||
| } | ||
|
|
||
| return count; | ||
| } | ||
|
|
||
| public int longestConsecutive(int[] nums) { | ||
| int count = 0; | ||
|
|
||
| // The Set has O(n) space complexity, | ||
| // because it may store up to n elements in memory. | ||
| // Is this the correct way to evaluate space complexity? | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵! 지금 분석하신 것처럼 주어진 것에서 추가적으로 사용된 공간에 대해서 분석하시면 됩니다! |
||
| HashSet<Integer> set = new HashSet<>(); | ||
| for (int num : nums) { | ||
| set.add(num); | ||
| } | ||
|
|
||
| for (int num : set) { | ||
| if (set.contains(num - 1)) { | ||
| continue; | ||
| } | ||
|
|
||
| int currentNum = num; | ||
| int currentCount = 1; | ||
|
|
||
| while (set.contains(currentNum + 1)) { | ||
| currentNum++; | ||
| currentCount++; | ||
| } | ||
|
|
||
| count = Math.max(count, currentCount); | ||
| } | ||
|
|
||
| return count; | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import java.util.HashMap; | ||
| import java.util.ArrayList; | ||
|
|
||
| class Solution { | ||
| public int[] topKFrequent(int[] nums, int k) { | ||
| // time complexity O(n) | ||
| // space complexity O(n) | ||
|
|
||
| HashMap<Integer, Integer> frequentMap = new HashMap<>(); | ||
|
|
||
| for (int i = 0; i < nums.length; i++) { | ||
| int num = nums[i]; | ||
| frequentMap.merge(num, 1, Integer::sum); | ||
| } | ||
|
|
||
| ArrayList<Integer>[] buckets = new ArrayList[nums.length + 1]; | ||
|
|
||
| for (int i = 0; i <= nums.length; i++) { | ||
| buckets[i] = new ArrayList<>(); | ||
| } | ||
|
|
||
| frequentMap.forEach((Integer a, Integer b) -> { | ||
| // Assign the largest values from the front of the array | ||
| buckets[nums.length - b].add(a); | ||
| }); | ||
|
|
||
| int[] result = new int[k]; | ||
|
|
||
| int pointer = 0; | ||
|
|
||
| for (int i = 0; i < buckets.length; i++) { | ||
| ArrayList<Integer> list = buckets[i]; | ||
| if (list.size() == 0) { | ||
| continue; | ||
| } | ||
| for (Integer num : list) { | ||
| result[pointer] = (int) num; | ||
| // Return the result when the pointer reaches k | ||
| if (pointer == (k - 1)) | ||
| return result; | ||
| pointer++; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import java.util.HashMap; | ||
|
|
||
| class Solution { | ||
| public int[] twoSum(int[] nums, int target) { | ||
| // Approach : using HashMap to get index with the element in O(n) time | ||
| // complexity | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick : 이부분은 time complexity 라고 명시해주는게 정확할 것 같습니다!
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵 줄바꿈이 조금 이상하게 적용되어있네요 |
||
| // SpaceComplexity is also O(n) | ||
| HashMap<Integer, Integer> numIndexMap = new HashMap<Integer, Integer>(); | ||
|
|
||
| // Make key and value HashMap | ||
| for (int i = 0; i < nums.length; i++) { | ||
| int num = nums[i]; | ||
| numIndexMap.put(num, i); | ||
| } | ||
|
|
||
| // Search for the other operand looping nums | ||
| for (int i = 0; i < nums.length; i++) { | ||
| int num = nums[i]; | ||
| int operand = target - num; | ||
| Integer index = numIndexMap.get(operand); | ||
| boolean indexExists = index != null; | ||
| boolean indexExistsAndIndexIsNotTheNum = indexExists && i != index; | ||
| if (indexExistsAndIndexIsNotTheNum) { | ||
| return new int[] { i, index }; | ||
| } | ||
| } | ||
|
|
||
| // If the valid answer is always exists this is not needed | ||
| // But the compiler don't know about that | ||
| return new int[2]; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
worst case 시간 복잡도에 대해서 생각해본적이 없었는데 리뷰하면서 공부할 기회가 되었습니다!