-
-
Notifications
You must be signed in to change notification settings - Fork 305
[mintheon] Week 1 #633
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
[mintheon] Week 1 #633
Changes from 6 commits
fe10e8f
308bb83
ff5969e
cb4e36b
a63ea90
c033942
8436024
3d815af
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,13 @@ | ||
| import java.util.Arrays; | ||
|
|
||
| class Solution { | ||
| public boolean containsDuplicate(int[] nums) { | ||
| Arrays.sort(nums); | ||
| for(int i = 1; i < nums.length; i++) { | ||
| if(nums[i] == nums[i - 1]) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| class Solution { | ||
| public int rob(int[] nums) { | ||
| int[] sums = new int[nums.length]; | ||
|
|
||
| sums[0] = nums[0]; | ||
|
|
||
| if (nums.length > 1) { | ||
| sums[1] = nums[1]; | ||
| } | ||
|
|
||
| if (nums.length > 2) { | ||
| sums[2] = nums[0] + nums[2]; | ||
| } | ||
|
|
||
| if (nums.length > 3) { | ||
| for (int i = 3; i < nums.length; i++) { | ||
| sums[i] = Math.max(nums[i] + sums[i - 2], nums[i] + sums[i - 3]); | ||
| } | ||
| } | ||
|
|
||
| int max = 0; | ||
| for(int sum : sums) { | ||
| max = Math.max(sum, max); | ||
| } | ||
|
|
||
| return max; | ||
| } | ||
| } |
DaleSeo marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| class Solution { | ||
| public int longestConsecutive(int[] nums) { | ||
| Set<Integer> numSet = new HashSet<>(); | ||
|
|
||
| for(int num : nums) { | ||
| numSet.add(num); | ||
| } | ||
|
|
||
| int longestSize = 0; | ||
|
|
||
| for(int num : numSet) { | ||
| if(!numSet.contains(num - 1)) { | ||
| int current = num; | ||
| int count = 1; | ||
|
|
||
| while(numSet.contains(current + 1)) { | ||
| count++; | ||
| current++; | ||
| } | ||
|
|
||
| longestSize = Math.max(count, longestSize); | ||
| } | ||
| } | ||
|
|
||
| return longestSize; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Map.Entry; | ||
| import java.util.PriorityQueue; | ||
|
|
||
| class Solution { | ||
| public int[] topKFrequent(int[] nums, int k) { | ||
| int[] answer = new int[k]; | ||
| Map<Integer, Integer> frequent = new HashMap<>(); | ||
|
|
||
| for(int num: nums) { | ||
| frequent.put(num, frequent.getOrDefault(num, 1) + 1); | ||
| } | ||
|
|
||
| PriorityQueue<Entry<Integer, Integer>> pq = new PriorityQueue<>((a, b) -> b.getValue().compareTo(a.getValue())); | ||
| pq.addAll(frequent.entrySet()); | ||
|
Comment on lines
+15
to
+16
Member
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. 가장 큰 수 k개만 필요한데 반드시 모든 숫자를 정렬해야할까를 고민해보시면 보다 효율적인 알고리즘을 얻으실 수도 있을 것 같습니다.
Member
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.
올려주신 풀이의 배열 방법은 생각치 못했어요. |
||
|
|
||
|
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. PriortyQueue랑 정렬은 시간 복잡도는 O(nlogn)으로 동일하지만, 상위 k개 요소를 구할 때는 PriorityQueue가 더 효율적이겠네요! 하나 배워갑니다 :) |
||
| for(int i = 0; i < k; i++) { | ||
| answer[i] = pq.poll().getKey(); | ||
| } | ||
|
|
||
| return answer; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| class Solution { | ||
| public boolean isPalindrome(String s) { | ||
| int start = 0; | ||
| int end = s.length() - 1; | ||
|
|
||
| while(start < end) { | ||
| if(!Character.isLetterOrDigit(s.charAt(start))) { | ||
| start++; | ||
| continue; | ||
| } | ||
|
|
||
| if(!Character.isLetterOrDigit(s.charAt(end))){ | ||
| end--; | ||
| continue; | ||
| } | ||
|
|
||
| if(Character.toLowerCase(s.charAt(start)) != Character.toLowerCase(s.charAt(end))) { | ||
| return false; | ||
| } | ||
|
|
||
| start++; | ||
| end--; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.