-
-
Notifications
You must be signed in to change notification settings - Fork 248
[sora0319] WEEK 01 solutions #1197
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
1210d88
Contains Duplicate solved
sora0319 8bcaad5
add newline
sora0319 0a4aa77
two sum solved
sora0319 c1045c8
Top k frequent elements solved
sora0319 f6bcd2d
longest consecutive sequence solved
sora0319 4a3f2dc
house rober solved
sora0319 7fe57bc
add newline
sora0319 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,12 @@ | ||
import java.util.*; | ||
class Solution { | ||
public boolean containsDuplicate(int[] nums) { | ||
Arrays.sort(nums); | ||
|
||
for(int i = 0; i < nums.length-1; i++){ | ||
if(nums[i] == nums[i+1]) 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,20 @@ | ||
import java.util.*; | ||
class Solution { | ||
public int rob(int[] nums) { | ||
int[] house = new int[nums.length]; | ||
Arrays.fill(house, -1); | ||
return maxRobbery(0, nums, house); | ||
} | ||
|
||
private int maxRobbery(int index, int[] nums, int[] house) { | ||
if (index >= nums.length) return 0; | ||
if (house[index] != -1) return house[index]; | ||
|
||
int rob = nums[index] + maxRobbery(index + 2, nums, house); | ||
int skip = maxRobbery(index + 1, nums, house); | ||
|
||
house[index] = Math.max(rob, skip); | ||
return house[index]; | ||
} | ||
} | ||
|
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,32 @@ | ||
import java.util.*; | ||
|
||
class Solution { | ||
public int longestConsecutive(int[] nums) { | ||
Set<Integer> checkList = new HashSet<>(); | ||
int seqCnt = 0; | ||
int start = Integer.MIN_VALUE; | ||
|
||
for(int n : nums){ | ||
checkList.add(n); | ||
} | ||
|
||
for (int n : nums) { | ||
int seq = 1; | ||
int target = n+1; | ||
if(checkList.contains(n-1))continue; | ||
|
||
while(checkList.contains(target)){ | ||
checkList.remove(target); | ||
seq++; | ||
target++; | ||
} | ||
|
||
if(seqCnt < seq){ | ||
seqCnt = seq; | ||
start = n; | ||
} | ||
} | ||
return seqCnt; | ||
} | ||
} | ||
|
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,26 @@ | ||
import java.util.*; | ||
|
||
class Solution { | ||
public int[] topKFrequent(int[] nums, int k) { | ||
Map<Integer,Integer> counts = new HashMap<>(); | ||
List<Integer> ordering = new ArrayList<>(); | ||
int[] results = new int[k]; | ||
|
||
for(int n : nums){ | ||
if(counts.containsKey(n)){ | ||
counts.put(n, counts.get(n)+1); | ||
continue; | ||
} | ||
counts.put(n, 1); | ||
ordering.add(n); | ||
} | ||
|
||
ordering.sort((o1,o2) -> counts.get(o2) - counts.get(o1)); | ||
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. 전체적으로 로직이 명확해서 이해하기 쉬웠습니다! 문제에서 추가로 원하는 방향이 O(nlogn)보다 더 최적화할 것을 요구하는데 아마 이 부분을 우선순위큐를 사용해서 하면 어떨까 싶습니다. |
||
for(int i = 0; i < k; i++){ | ||
results[i] = ordering.get(i); | ||
} | ||
|
||
return results; | ||
} | ||
} | ||
|
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.*; | ||
class Solution { | ||
public int[] twoSum(int[] nums, int target) { | ||
Map<Integer,Integer> element = new HashMap<>(); | ||
|
||
for(int i = 0; i < nums.length; i++){ | ||
element.put(i, nums[i]); | ||
} | ||
|
||
int[] result = new int[2]; | ||
int n = 0; | ||
for(int i = 0; i < nums.length; i++){ | ||
element.remove(i); | ||
n = target - nums[i]; | ||
if(element.containsValue(n)){ | ||
result[0] = i; | ||
break; | ||
} | ||
} | ||
|
||
for(int i = 0; i < nums.length; i++){ | ||
if(nums[i] == n && i != result[0]){ | ||
result[1] = i; | ||
break; | ||
} | ||
} | ||
return result; | ||
} | ||
} | ||
|
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.
전체적으로 코드를 잘 짜신 것 같습니다!
Arrays.sort(nums)를 통해서 배열을 정렬한 뒤 인접한 원소끼리 비교하는 방식은 시간 복잡도가 평균 O(nlogn)으로 나옵니다.
이 문제같은 경우는 단순 중복 존재 여부이기 때문에 Set을 사용하는 방식을 한번 고민해 보시면 좋을 것 같습니다!
Set을 사용하게 되면 각 원소를 한 번만 순회하면서 중복 여부를 O(n)으로 판단할 수 있습니다:)