Repository navigation
[JeonBeomGu-S] WEEK 01 Solutions #2923
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
base: main
Are you sure you want to change the base?
Changes from all commits
7e87bc4
abcd437
00e0bc0
c9cb065
4e5fcc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * 시간 복잡도: O(n) - nums 배열 길이인 n번 만큼 반복문이 반복될 수 있음 | ||
| * 공간 복잡도: O(n) - set에 데이터를 저장할 때 nums 배열 길이인 n개의 공간만큼 추가 공간이 필요 | ||
| */ | ||
| public class Solution { | ||
| public boolean containsDuplicate(int[] nums) { | ||
| Set<Integer> set = new HashSet<>(); | ||
| for (int i = 0; i < nums.length; i++) { | ||
| if (set.contains(nums[i])) { | ||
| return true; | ||
| } else { | ||
| set.add(nums[i]); | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * 시간 복잡도: O(nlogn) - 배열의 길이 n 만큼 반복문과 정렬 | ||
| * 공간 복잡도: O(n) - 배열의 길이 n 만큼의 맵, 리스트와 답을 저장할 배열의 크기 k | ||
| * k는 최대 n을 넘지 못하므로 무시 | ||
| */ | ||
|
|
||
| class Solution { | ||
| public int[] topKFrequent(int[] nums, int k) { | ||
| Map<Integer, Integer> map = new HashMap(); | ||
| for (int num : nums) { | ||
| if (map.containsKey(num)) { | ||
| map.put(num, map.get(num) + 1); | ||
| } else { | ||
| map.put(num, 1); | ||
| } | ||
| } | ||
|
|
||
| List<Integer> list = new ArrayList<>(map.keySet()); | ||
| list.sort((a, b) -> map.get(b) - map.get(a)); | ||
|
Contributor
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. 자바는 이렇게 두 값을 비교하는 함수를 넘겨서 정렬하는 게 신기하네요. 파이썬은 기준값(key)을 넘기고 reverse=True로 뒤집는 방식인데, 찾아보니 언어마다 정렬 방법이 달라서 푸신 방법처럼 두 값을 비교하는 함수를 직접 넘기는 방식은 파이썬에서는 바로 쓸 수 없다네요. 배워갑니다! |
||
|
|
||
| int[] answer = new int[k]; | ||
| for (int i = 0; i < k; i++) { | ||
| answer[i] = list.get(i); | ||
| } | ||
|
|
||
| return answer; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| 시간 복잡도: O(n) - 최대 배열의 길이 n만큼 반복 | ||
| 공간 복잡도: O(n) - 최대 배열의 길이 n만큼의 map 크기 사용 | ||
| */ | ||
| class Solution { | ||
| public int[] twoSum(int[] nums, int target) { | ||
| Map<Integer, Integer> map = new HashMap<>(); | ||
| int[] answer = new int[2]; | ||
|
|
||
| for (int i = 0; i < nums.length; i++) { | ||
| if (map.containsKey(target - nums[i])) { | ||
|
Contributor
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. 저도 같은 방법으로 풀어서 반갑네요! 처음엔 숫자를 전부 map에 넣어두고 찾는 방법으로 했는데, 그러면 자기 자신을 짝으로 고르지 않게 인덱스를 한 번 더 확인해야 하더라고요. 넣기 전에 먼저 찾는 이 방법이 더 나은 것 같아요 👍 |
||
| answer[0] = map.get(target - nums[i]); | ||
| answer[1] = i; | ||
| // 찾은 경우 바로 return | ||
| return answer; | ||
| } else { | ||
| map.put(nums[i], i); | ||
| } | ||
| } | ||
|
|
||
| // 못 찾은 경우 null | ||
| return null; | ||
| } | ||
| } | ||
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.
k는 n을 넘을 수 없어서 O(n)에 포함된다고 적어주셔서, 공간 복잡도가 왜 O(n)인지 바로 이해되네요!