-
-
Notifications
You must be signed in to change notification settings - Fork 247
[eunhwa99] Week01 Solutions #1153
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
8b8188b
contains duplicate
3502dd4
two sum
174b4de
Merge branch 'DaleStudy:main' into main
eunhwa99 62532ae
top k frequent elements
808c2c0
Merge remote-tracking branch 'origin/main'
f580fba
longest consecutive sequence
eeafd95
개행 추가
3cf9df1
house robber
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 |
---|---|---|
@@ -1,14 +1,15 @@ | ||
import java.util.HashSet; | ||
|
||
// 공간 복잡도; O(n) | ||
// 시간 복잡도: O(1) | ||
class Solution { | ||
public boolean containsDuplicate(int[] nums) { | ||
HashSet<Integer> seen = new HashSet<>(); | ||
HashSet<Integer> visited = new HashSet<>(); | ||
for (int num : nums) { | ||
if (!seen.add(num)) { | ||
if (!visited.add(num)) { | ||
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 |
---|---|---|
@@ -1,24 +1,21 @@ | ||
import java.util.Arrays; | ||
|
||
// 시간 복잡도: O(n) - dp 배열을 한 번 순회 | ||
// 공간 복잡도: O(n) - dp 배열 | ||
class Solution { | ||
int size = 0; | ||
int[] numArray; | ||
int[] dp; | ||
|
||
public int rob(int[] nums) { | ||
size = nums.length; | ||
dp = new int[size]; | ||
// 배열의 모든 값을 -1로 변경 | ||
Arrays.fill(dp, -1); | ||
numArray = nums; | ||
return fun(0); | ||
} | ||
int[][] dp = new int[nums.length][2]; // 0: not robbed, 1: robbed | ||
|
||
if(nums.length == 0) return 0; | ||
if(nums.length == 1) return nums[0]; | ||
|
||
private int fun(int idx) { | ||
if (idx >= size) return 0; | ||
if (dp[idx] != -1) return dp[idx]; | ||
dp[idx] = 0; // check | ||
dp[idx] += Math.max(fun(idx + 2) + numArray[idx], fun(idx + 1)); | ||
return dp[idx]; | ||
dp[0][0] = 0; | ||
dp[0][1] = nums[0]; | ||
|
||
for(int i = 1; i < nums.length; i++){ | ||
dp[i][0] = Math.max(dp[i-1][0], dp[i-1][1]); | ||
dp[i][1] = dp[i-1][0] + nums[i]; | ||
} | ||
|
||
return Math.max(dp[nums.length-1][0], dp[nums.length-1][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 |
---|---|---|
@@ -1,23 +1,23 @@ | ||
import java.util.HashSet; | ||
import java.util.Arrays; | ||
|
||
class Solution { | ||
public int longestConsecutive(int[] nums) { | ||
HashSet<Integer> mySet = new HashSet<Integer>(); | ||
|
||
for (int num : nums) { | ||
mySet.add(num); | ||
} | ||
|
||
int result = 0; | ||
for (int num : mySet) { | ||
int cnt = 1; | ||
if (!mySet.contains(num - 1)) { | ||
while (mySet.contains(++num)) { | ||
++cnt; | ||
} | ||
result = Math.max(cnt, result); | ||
// 시간 복잡도: O(nlogn) - 정렬 | ||
// 공간 복잡도: O(1) | ||
class Solution{ | ||
public int longestConsecutive(int[] nums){ | ||
if(nums.length == 0) return 0; | ||
Arrays.sort(nums); | ||
int pre = nums[0]; | ||
int max = 1; | ||
int count = 1; | ||
for(int i = 1; i < nums.length; i++){ | ||
if(nums[i] == pre + 1){ | ||
count++; | ||
max = Math.max(max, count); | ||
} else if(nums[i] != pre){ | ||
count = 1; | ||
} | ||
pre = nums[i]; | ||
} | ||
return result; | ||
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 |
---|---|---|
@@ -1,16 +1,33 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.PriorityQueue; | ||
|
||
// 시간 복잡도: O(nlogk) - 최대 힙에 k개의 요소를 넣는데 O(logk)가 걸리고, 이를 n번 반복 | ||
// 공간 복잡도: O(n) - 빈도수를 저장하는 freqMap | ||
class Solution { | ||
public static int[] topKFrequent(int[] nums, int k) { | ||
Map<Integer, Integer> myMap = new HashMap<>(); | ||
for (int num : nums) { | ||
myMap.put(num, myMap.getOrDefault(num, 0) + 1); | ||
} | ||
return myMap.entrySet() | ||
.stream() | ||
.sorted((v1, v2) -> Integer.compare(v2.getValue(),v1.getValue())) | ||
.map(Map.Entry::getKey) | ||
.mapToInt(Integer::intValue) | ||
.toArray(); | ||
|
||
public int[] topKFrequent(int[] nums, int k) { | ||
Map<Integer, Integer> freqMap = new HashMap<>(); | ||
for (int num : nums) { | ||
freqMap.put(num, freqMap.getOrDefault(num, 0) + 1); | ||
} | ||
|
||
PriorityQueue<Map.Entry<Integer, Integer>> maxHeap = | ||
new PriorityQueue<>((a, b) -> b.getValue() - a.getValue()); | ||
|
||
// 빈도수를 기준으로 최소 힙에 k개의 요소를 넣기 | ||
for (Map.Entry<Integer, Integer> entry : freqMap.entrySet()) { | ||
maxHeap.offer(entry); | ||
} | ||
|
||
int[] result = new int[k]; | ||
int index = 0; | ||
while (k > 0) { | ||
k--; | ||
result[index++] = maxHeap.poll().getKey(); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
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 |
---|---|---|
@@ -1,80 +1,41 @@ | ||
// 문제 요구사항 -> O(n^2) 보다 작은 시간복잡도로 구현 필요 | ||
// 문제를 보자마자 생각난 것 -> 이분탐색 (2 원소 합이 target 인 것을 구해야 하므로) | ||
// 이분 탐색 시간 복잡도 -> 정렬(O(nlogn)) + 이분탐색 (log(n)) -> nlogn | ||
// 공간 복잡도 -> 배열 크기 만큼 공간 필요 (n) | ||
// two pointer | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Comparator; | ||
|
||
class ValueIndex implements Comparable<ValueIndex> { | ||
int value; | ||
class Point { | ||
int index; | ||
int value; | ||
Point(int index, int value){ | ||
this.index = index; | ||
this.value = value;} | ||
|
||
// ValueIndex 객체를 정렬할 때 value 기준으로 오름차순 정렬 | ||
@Override | ||
public int compareTo(ValueIndex other) { | ||
return Integer.compare(this.value, other.value); | ||
} | ||
} | ||
|
||
class Solution { | ||
|
||
// 시간 복잡도: O(nlogn) - 정렬 | ||
// 공간 복잡도: O(n) - Point 객체 배열 | ||
class Solution{ | ||
public int[] twoSum(int[] nums, int target) { | ||
int size = nums.length; | ||
ValueIndex[] valueIndex = new ValueIndex[size]; | ||
for (int i = 0; i < size; ++i) { | ||
valueIndex[i] = new ValueIndex(); | ||
valueIndex[i].value = nums[i]; | ||
valueIndex[i].index = i; // 정렬 전 original index 저장 | ||
Point[] points = new Point[nums.length]; | ||
for(int i = 0; i < nums.length; i++){ | ||
points[i] = new Point(i, nums[i]); | ||
} | ||
Arrays.sort(valueIndex); // 정렬 | ||
Arrays.sort(points, Comparator.comparingInt(p -> p.value)); | ||
int[] result = new int[2]; | ||
int left = 0; | ||
int right = nums.length - 1; | ||
|
||
while (left < right) { | ||
int leftVal = valueIndex[left].value; | ||
int rightVal = valueIndex[right].value; | ||
int sum = leftVal + rightVal; | ||
|
||
if (sum < target) { // target 보다 합이 작으면, left 값이 커져야 함 | ||
left += 1; | ||
} else if (sum > target) { | ||
right -= 1; // target 보다 합이 크면, right 값이 작아져야 함 | ||
} else { // sum = target 이면 끝! | ||
break; | ||
} | ||
} | ||
|
||
return new int[]{valueIndex[left].index, valueIndex[right].index}; | ||
} | ||
} | ||
|
||
|
||
/** | ||
* hashMap을 이용한 O(n) 방법도 있다고 해서 추가 구현해보았습니다. (시간/공간 복잡도 O(n)) | ||
*/ | ||
|
||
class Solution { | ||
|
||
public int[] twoSum(int[] nums, int target) { | ||
HashMap<Integer, Integer> numMap = new HashMap<>(); | ||
int left = 0, right = 0; | ||
for (int i = 0; i < nums.length; i++) { | ||
int complement = target - nums[i]; // 현재 숫자와 합쳐서 target을 만드는 숫자 | ||
|
||
// 이미 그 숫자가 해시맵에 있다면, 두 인덱스를 반환 | ||
if (numMap.containsKey(complement)) { | ||
left = numMap.get(complement); | ||
right = i; | ||
int sum = points[left].value + points[right].value; | ||
if (sum == target) { | ||
result[0] = points[left].index; | ||
result[1] = points[right].index; | ||
break; | ||
} else if (sum < target) { | ||
left++; | ||
} else { | ||
right--; | ||
} | ||
|
||
// 해시맵에 현재 숫자와 인덱스를 추가 | ||
numMap.put(nums[i], i); | ||
} | ||
|
||
return new int[]{left, right}; | ||
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.