-
-
Notifications
You must be signed in to change notification settings - Fork 195
[sm9171] Week01 Solutions #1158
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
8 commits
Select commit
Hold shift + click to select a range
894505b
feat:contains-duplicate solution
sm9171 c53e52c
feat:two-sum solution
sm9171 e68bd88
feat: top-k-frequent-elements solution
sm9171 918bba5
feat: longest Consecutive Sequence solution
sm9171 b249de9
style: Add newline character
sm9171 115c0e3
feat: house-robber solution
sm9171 23984e6
refactor: contains-duplicate solution
sm9171 6df5759
refactor: top-k-frequent-elements solution
sm9171 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,13 @@ | ||
class Solution { | ||
public boolean containsDuplicate(int[] nums) { | ||
HashSet<Integer> set = new HashSet<>(); | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
if (set.contains(nums[i])) { | ||
return true; | ||
} | ||
set.add(nums[i]); | ||
} | ||
return false; | ||
} | ||
} |
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 @@ | ||
class Solution { | ||
public int rob(int[] nums) { | ||
if (nums.length == 0) return 0; | ||
if (nums.length == 1) return nums[0]; | ||
|
||
nums[1] = Math.max(nums[0], nums[1]); | ||
|
||
for (int i = 2; i < nums.length; i++) { | ||
nums[i] = Math.max(nums[i - 1], nums[i - 2] + nums[i]); | ||
} | ||
|
||
return nums[nums.length - 1]; | ||
} | ||
} |
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,20 @@ | ||
class Solution { | ||
public int longestConsecutive(int[] nums) { | ||
int longest = 0; | ||
Set<Integer> set = Arrays.stream(nums) | ||
.boxed() | ||
.collect(Collectors.toSet()); | ||
|
||
for (Integer i : set) { | ||
if (set.contains(i - 1)) continue; | ||
int length = 1; | ||
|
||
while (set.contains(i + 1)) { | ||
length++; | ||
i++; | ||
} | ||
longest = Math.max(longest, length); | ||
} | ||
return longest; | ||
} | ||
} |
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,21 @@ | ||
class Solution { | ||
public static int[] topKFrequent(int[] nums, int k) { | ||
Map<Integer, Integer> hashMap = new HashMap<>(); | ||
for (int i = 0; i < nums.length; i++) { | ||
int num = nums[i]; | ||
hashMap.put(num, hashMap.getOrDefault(num, 0) + 1); | ||
} | ||
|
||
List<Integer> list = hashMap.entrySet().stream() | ||
.sorted(Map.Entry.<Integer, Integer>comparingByValue().reversed()) | ||
.limit(k) | ||
.map(Map.Entry::getKey) | ||
.toList(); | ||
|
||
int[] result = list.stream() | ||
.mapToInt(Integer::intValue) | ||
.toArray(); | ||
|
||
return result; | ||
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. 위에 List list 부분에서 stream을 사용하신 것 처럼, 반환값도 stream을 활용해보시는 건 어떨까요? 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. 네 array로 타입 변경하는 부분도 스트림을 이용하여 해결해 보았습니다 :) |
||
} | ||
} |
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,15 @@ | ||
class Solution { | ||
public int[] twoSum(int[] nums, int target) { | ||
Map<Integer, Integer> hashMap = new HashMap<>(); | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
int gap = target - nums[i]; | ||
if(hashMap.containsKey(gap)){ | ||
int j = hashMap.get(gap); | ||
return new int[]{i,j}; | ||
} | ||
hashMap.put(nums[i], i); | ||
} | ||
return null; | ||
} | ||
} |
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.
add 할 때 존재하는지 확인하고 바로 return 하는 건 어떨까요?
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.
wisdon08님이 좋은 의견 남겨주신것에 저도 동의합니다!
현재는 배열을 끝까지 순회한 후에 중복을 판단하는 구조인것 같아서, 중복을 미리 찾고 빠르게 결과 반환을 하는 구조로 변경하는 것도 좋아 보입니다 😀
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.
두 분 의견 감사합니다. 얘기 해주신대로 얼리 리턴을 하니 17ms에서 12ms로 단축 되었네요 :)