-
-
Notifications
You must be signed in to change notification settings - Fork 249
[mumunuu] WEEK 01 solutions #1710
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
6 commits
Select commit
Hold shift + click to select a range
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,28 @@ | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
|
||
class Solution { | ||
|
||
/** | ||
* | ||
* Set 판단으로 O(n) | ||
* | ||
* */ | ||
public boolean containsDuplicate(int[] nums) { | ||
|
||
Set<Integer> set = new HashSet<>(); | ||
|
||
for (int num : nums) { | ||
|
||
boolean isAdded = set.add(num); | ||
|
||
if (!isAdded) { | ||
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,41 @@ | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
class Solution { | ||
|
||
|
||
// priority queue 로 풀어보기 | ||
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); | ||
} | ||
|
||
// 갯수만큼 배열 생성 (빈도수를 인덱스로 가지는) | ||
List<Integer>[] bucket = new List[nums.length + 1]; // freq는 최대 nums.length | ||
for (int i = 0; i < bucket.length; i++) { | ||
bucket[i] = new ArrayList<>(); | ||
} | ||
|
||
for (Map.Entry<Integer, Integer> entry : freqMap.entrySet()) { | ||
int num = entry.getKey(); | ||
int freq = entry.getValue(); | ||
bucket[freq].add(num); | ||
} | ||
|
||
// 빈도수가 높은 뒤에서부터 넣어줌 | ||
List<Integer> result = new ArrayList<>(); | ||
for (int i = bucket.length - 1; i >= 0 && result.size() < k; i--) { | ||
if (!bucket[i].isEmpty()) { | ||
result.addAll(bucket[i]); | ||
} | ||
} | ||
|
||
// k개만 반환 | ||
return result.subList(0, k).stream().mapToInt(i -> i).toArray(); | ||
} | ||
} |
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,46 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
class Solution { | ||
/** | ||
* 배열의 요소 2개를 더해서 target 숫자들을 만들고 인덱스를 반환 | ||
* 같은 숫자를 여러번 사용할 수 없고, 해답은 반드시 존재함 | ||
* Follow-up: Can you come up with an algorithm that is less than O(n^2) time complexity? | ||
*/ | ||
|
||
/* | ||
// naive 한 풀이법. O(n^2) | ||
public int[] twoSum(int[] nums, int target) { | ||
// naive 한 방법: 나를 제외한 나머지 숫자가 있는지 찾음 | ||
for (int i=0; i<nums.length-1; i++) { | ||
for (int k=i+1; k<nums.length; k++) { | ||
if (nums[i] + nums[k] == target) { | ||
return new int[]{i,k}; | ||
} | ||
} | ||
} | ||
return new int[]{}; | ||
} | ||
*/ | ||
|
||
|
||
// 처음에 Map<Integer, List<Integer>>라고 생각했지만 그냥 Integer, Integer로 가능함 | ||
public int[] twoSum(int[] nums, int target) { | ||
|
||
Map<Integer, Integer> map = new HashMap<>(); | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
|
||
int needValue = target - nums[i]; | ||
if (map.containsKey(needValue)) { | ||
return new int[]{map.get(needValue), i}; | ||
} | ||
map.put(nums[i], i); // 항상 현재 인덱스를 나중에 저장 | ||
|
||
} | ||
|
||
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.
set.add 의 반환 값을 이용해서 확인하셨군요! ㅎㅎ