-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
Merged
Merged
[mintheon] Week 1 #633
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fe10e8f
valid-palindrome solution
mintheon 308bb83
contains-duplicate solution
mintheon ff5969e
top-k-frequent-elements solution
mintheon cb4e36b
fix: 코드 마지막 개행처리
mintheon a63ea90
longest-consecutive-sequence solved
mintheon c033942
house-robber solved
mintheon 8436024
longest-consecutive-sequence로 이동
mintheon 3d815af
contains-duplicate Set 방식으로 변경
mintheon 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
class Solution { | ||
public boolean containsDuplicate(int[] nums) { | ||
Set<Integer> numSet = new HashSet(); | ||
|
||
for(int num : nums) { | ||
numSet.add(num); | ||
} | ||
|
||
return numSet.size() != nums.length; | ||
} | ||
} |
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 |
---|---|---|
@@ -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; | ||
} | ||
} |
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 |
---|---|---|
@@ -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; | ||
} | ||
} |
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 |
---|---|---|
@@ -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()); | ||
|
||
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; | ||
} | ||
} |
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 |
---|---|---|
@@ -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; | ||
} | ||
} |
Oops, something went wrong.
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.
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.
가장 큰 수 k개만 필요한데 반드시 모든 숫자를 정렬해야할까를 고민해보시면 보다 효율적인 알고리즘을 얻으실 수도 있을 것 같습니다.
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.
올려주신 풀이의 배열 방법은 생각치 못했어요.
다만 자바로 변환해서 푸니 List<List<>> 방식이 되어 코드가 복잡해지는 경향이 있는것 같아요. 그래도 속도는 확실하네요!! 👍🏻