-
-
Notifications
You must be signed in to change notification settings - Fork 195
[JEONGBEOMKO] WEEK 01 solutions #1157
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
7 commits
Select commit
Hold shift + click to select a range
f39a12a
containsDuplicate
JEONGBEOMKO 405949d
two-sum solution
JEONGBEOMKO 8031e40
add solution of top-k-frequent-elements
JEONGBEOMKO 4212d9b
add solution of longest-consecutive-sequence
JEONGBEOMKO 6c38ca5
[Refactor] longest-consecutive-sequence
JEONGBEOMKO aa154ae
add solution of house-robber
JEONGBEOMKO f8035d1
[Refactor] longest-consecutive-sequence
JEONGBEOMKO 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,17 @@ | ||
import java.util.HashSet; | ||
|
||
class Solution { | ||
public boolean containsDuplicate(int[] nums) { | ||
HashSet<Integer> numSet = new HashSet<>(); | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
|
||
if (numSet.contains(nums[i])) { | ||
return true; | ||
} | ||
numSet.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,16 @@ | ||
class Solution { | ||
public int rob(int[] nums) { | ||
if (nums.length == 1) { | ||
return nums[0]; | ||
} | ||
|
||
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(dp[i - 1], dp[i - 2] + nums[i]); | ||
} | ||
return dp[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,22 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
class Solution { | ||
|
||
public boolean containsDuplicate(int[] nums) { | ||
|
||
Map<Integer, Integer> countMap = new HashMap<>(); | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
countMap.put(nums[i], countMap.getOrDefault(nums[i] , 0) + 1); | ||
} | ||
|
||
for (Map.Entry<Integer, Integer> map : countMap.entrySet()) { | ||
if (map.getValue() >= 2) { | ||
return true; | ||
} | ||
} | ||
|
||
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,29 @@ | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
|
||
class Solution { | ||
public int[] topKFrequent(int[] nums, int k) { | ||
|
||
Map<Integer, Integer> frequencyMap = new HashMap<>(); | ||
for(int num : nums) { | ||
frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1); | ||
} | ||
|
||
Map.Entry<Integer, Integer>[] arr = new Map.Entry[frequencyMap.size()]; | ||
Iterator<Map.Entry<Integer, Integer>> iterator = frequencyMap.entrySet().iterator(); | ||
for (int i=0; i<arr.length; i++) { | ||
arr[i] = iterator.next(); | ||
} | ||
|
||
Arrays.sort(arr, (e1, e2) -> e2.getValue() - e1.getValue()); | ||
Comment on lines
+14
to
+20
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. stream을 이용하여 가독성을 높이는 방법도 좋아보입니다. |
||
|
||
int[] answer = new int[k]; | ||
for (int i=0; i<k; i++) { | ||
answer[i] = arr[i].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,20 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
class Solution { | ||
public int[] twoSum(int[] nums, int target) { | ||
Map<Integer, Integer> numberMap = new HashMap<>(); | ||
for (int i=0; i<nums.length; i++) { | ||
numberMap.put(nums[i], i); | ||
} | ||
|
||
for(int i=0; i<nums.length; i++) { | ||
int operand = target - nums[i]; | ||
if (numberMap.containsKey(operand) && numberMap.get(operand) != i) { // 자기 자신은 제외 | ||
return new int[] { numberMap.get(target - nums[i]), i }; | ||
} | ||
} | ||
Comment on lines
+5
to
+16
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. 처음 for문을 실행 할때 분기처리로 값을 리턴해도 좋을 것 같아요 |
||
|
||
return new int[] {}; | ||
} | ||
} |
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.
getOrDefault 메소드를 사용해서 초기값을 설정해 주었군요!