-
-
Notifications
You must be signed in to change notification settings - Fork 245
[suhyenim] WEEK01 solutions #1726
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 1 commit
Commits
Show all changes
2 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,65 @@ | ||
/* [5th/week01] 217. Contains Duplicate | ||
|
||
1. 문제 요약 | ||
링크: https://leetcode.com/problems/contains-duplicate/ | ||
배열에서 동일한 숫자가 2번 이상 존재하면 true 반환, 아니면 false 반환 | ||
|
||
2. 풀이 로직 | ||
제출1: 반복문을 중첩으로 돌리면서, 동일한 값을 발견하면 바로 true 반환 | ||
실패: Time Limit Exceeded 발생 | ||
=> 시간 복잡도: O(n^2), 공간 복잡도: O(1) | ||
class Solution { | ||
public boolean containsDuplicate(int[] nums) { | ||
for (int i = 0; i < nums.length; i++) { | ||
for (int j = i + 1; j < nums.length; j++) { | ||
if (nums[i] == nums[j]) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
풀이2: 크기를 오름차순으로 정렬해서, 바로 옆 숫자랑 겹치는지 확인 | ||
suhyenim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
성공: Time: 21 ms (19.91%), Space: 56.4 MB (98.12%) | ||
=> 시간 복잡도: O(nlog(n)), 공간 복잡도: O(1) | ||
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; | ||
} | ||
} | ||
|
||
풀이3: 배열을 순회하면서 HashSet에 겹치는 숫자가 존재하면 true 반환, 아니라면 HashSet에 본(seen) 숫자로 추가 | ||
성공: Time: 14 ms (61.51%), Space: 58.2 MB (61.9%) | ||
=> 시간 복잡도: O(n), 공간 복잡도: O(n) | ||
class Solution { | ||
public boolean containsDuplicate(int[] nums) { | ||
HashSet<Integer> seen = new HashSet<>(); | ||
for (int i = 0; i < nums.length; i++) { | ||
if (seen.contains(nums[i])) return true; | ||
seen.add(nums[i]); | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
3. TIL | ||
그리고 메모리가 충분하고 빠른 속도가 필요할 때, HashSet을 사용해볼 수 있다. | ||
suhyenim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
하지만 메모리가 여유롭지 않다면, HashSet을 사용하는 방식보다는 기존의 정렬을 사용하는 방식이 알맞을 수 있다. | ||
|
||
*/ | ||
|
||
import java.util.Arrays; | ||
|
||
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,61 @@ | ||
/* [5th/week01] 347. Top K Frequent Elements | ||
|
||
1. 문제 요약 | ||
링크: https://leetcode.com/problems/top-k-frequent-elements/description/ | ||
가장 빈도가 높은 k개의 숫자를 배열로 반환 | ||
suhyenim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
2. 풀이 로직 | ||
풀이1: | ||
성공: Time: 17 ms (12.72%), Space: 48.9 MB (34.62%) | ||
=> 시간 복잡도: O(nlogn), 공간 복잡도: O(n) | ||
|
||
class Solution { | ||
public int[] topKFrequent(int[] nums, int k) { | ||
Map<Integer, Integer> count = new HashMap<>(); | ||
for(int n : nums) { | ||
if(!count.containsKey(n)) { | ||
count.put(n, 1); | ||
} else { | ||
count.put(n, count.get(n) + 1); | ||
} | ||
} | ||
|
||
List<Integer> keys = new ArrayList<>(count.keySet()); | ||
keys.sort((a, b) -> count.get(b) - count.get(a)); | ||
|
||
int[] answer = new int[k]; | ||
for (int i = 0; i < k; i++){ | ||
answer[i] = keys.get(i); | ||
} | ||
|
||
return answer; | ||
} | ||
} | ||
|
||
3. TIL | ||
value값 기준으로 key를 내림차순 정렬하는 방법: keys.sort((a, b) -> count.get(b) - count.get(a)); | ||
suhyenim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
*/ | ||
|
||
class Solution { | ||
public int[] topKFrequent(int[] nums, int k) { | ||
Map<Integer, Integer> count = new HashMap<>(); | ||
for(int n : nums) { | ||
if(!count.containsKey(n)) { | ||
count.put(n, 1); | ||
} else { | ||
count.put(n, count.get(n) + 1); | ||
} | ||
} | ||
|
||
List<Integer> keys = new ArrayList<>(count.keySet()); | ||
keys.sort((a, b) -> count.get(b) - count.get(a)); | ||
|
||
int[] answer = new int[k]; | ||
for (int i = 0; i < k; i++){ | ||
answer[i] = keys.get(i); | ||
} | ||
|
||
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,57 @@ | ||
/* [5th/week01] 1. Two Sum | ||
|
||
1. 문제 요약 | ||
링크: https://leetcode.com/problems/two-sum/description/ | ||
배열 내 두 숫자의 합이 target이 되는 key들을 배열로 반환 | ||
(특이점: 답은 반드시 하나만 존재) | ||
|
||
2. 풀이 로직 | ||
제출1: 반복문을 중첩으로 돌리면서, 두 숫자의 합이 target일 때의 두 인덱스를 배열로 반환 | ||
성공: Time: 45 ms (30.61%), Space: 44.9 MB (63.79%) | ||
=> 시간 복잡도: O(n^2), 공간 복잡도: O(1) | ||
class Solution { | ||
public int[] twoSum(int[] nums, int target) { | ||
for (int i = 0; i < nums.length - 1; i++) { | ||
for (int j = i + 1; j < nums.length; j++) { | ||
if (nums[i] + nums[j] == target) | ||
return new int[]{i, j}; | ||
} | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
풀이2: target과의 차가 HashMap의 key들 중에 존재하면 두 인덱스를 배열로 반환, 아니라면 HashMap에 value-key쌍으로 추가 | ||
성공: 2 ms (98.95%), Space: 45.1 MB (30.45%) | ||
=> 시간 복잡도: O(n), 공간 복잡도: O(n) | ||
class Solution { | ||
public int[] twoSum(int[] nums, int target) { | ||
Map<Integer, Integer> indices = new HashMap<>(); | ||
for (int i = 0; i < nums.length; i++) { | ||
int complement = target - nums[i]; | ||
if (indices.containsKey(complement)) { | ||
int j = indices.get(complement); | ||
return new int[]{j, i}; | ||
} | ||
indices.put(nums[i], i); | ||
suhyenim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return null; | ||
} | ||
} | ||
|
||
3. TIL | ||
HashTable을 활용해서 key-value 자료구조를 구현할 수 있다. | ||
|
||
*/ | ||
|
||
class Solution { | ||
suhyenim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public int[] twoSum(int[] nums, int target) { | ||
for (int i = 0; i < nums.length - 1; i++) { | ||
for (int j = i + 1; j < nums.length; j++) { | ||
if (nums[i] + nums[j] == target) | ||
return new int[]{i, j}; | ||
} | ||
} | ||
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.
Uh oh!
There was an error while loading. Please reload this page.