-
-
Notifications
You must be signed in to change notification settings - Fork 360
[hyunpill3] WEEK 01 Solutions #2666
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
Changes from all commits
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,12 @@ | ||
| class Solution { | ||
| public boolean containsDuplicate(int[] nums) { | ||
| HashSet<Integer> set = new HashSet<>(); | ||
| for (int num : nums) { | ||
| if (set.contains(num)) { | ||
| return true; | ||
| } | ||
| set.add(num); | ||
| } | ||
| return false; | ||
| } | ||
| } |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 상태를 두 변수로 유지하며 선형 시간에 해결합니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| class Solution { | ||
| public int rob(int[] nums) { | ||
| int next = 0; | ||
| int next2 = 0; | ||
|
Comment on lines
+3
to
+4
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. 공간복잡도를 O(1) 로 풀어내신 점이 인상적이네요! 👍 |
||
|
|
||
| for (int num : nums) { | ||
| int current = Math.max(next, next2 + num); | ||
|
|
||
| next2 = next; | ||
| next = current; | ||
| } | ||
|
|
||
| return next; | ||
| } | ||
| } | ||
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 각 원소를 한 번씩 검사하고, 시작점 여부를 확인하여 전체를 탐색합니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| class Solution { | ||
| public int longestConsecutive(int[] nums) { | ||
| Set<Integer> set = new HashSet<>(); | ||
| for (int num : nums) { | ||
| set.add(num); | ||
| } | ||
|
|
||
| int res = 0; | ||
|
|
||
| for (int num : set) { | ||
| if (!set.contains(num - 1)) { | ||
| int current = num; | ||
| int count = 1; | ||
|
|
||
| while (set.contains(current + 1)) { | ||
| current++; | ||
| count++; | ||
| } | ||
|
|
||
| res = Math.max(res, count); | ||
| } | ||
| } | ||
|
|
||
| return res; | ||
| } | ||
| } |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 스트림 기반 복잡도는 정렬에 의해 결정되며 구현은 간결합니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| class Solution { | ||
| public int[] topKFrequent(int[] nums, int k) { | ||
| List<Integer> list = new ArrayList<>(); | ||
| Arrays.stream(nums) | ||
| .boxed() | ||
| .collect(Collectors.groupingBy(x -> x)) | ||
| .entrySet() | ||
| .stream() | ||
| .sorted(Map.Entry.comparingByValue((o1, o2) -> Integer.compare(o2.size(), o1.size()))) | ||
| .forEachOrdered(x -> { | ||
| if (list.size() < k) | ||
| list.add(x.getKey()); | ||
| }); | ||
|
|
||
| return list.stream().mapToInt(Integer::intValue).toArray(); | ||
| } | ||
| } |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 한 번의 순회로 해결하며 최적의 보조 공간을 사용합니다. 개선 제안: 마지막에 누락된 return 문이 필요해 보완 권장: 모든 경우를 커버하도록 종료 시 반환값을 추가.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| class Solution { | ||
| public int[] twoSum(int[] nums, int target) { | ||
| HashMap<Integer, Integer> map = new HashMap<>(); | ||
| for (int i = 0; i < nums.length; i++) { | ||
| int num2 = target - nums[i]; | ||
| if (map.containsKey(num2)) { | ||
| return new int[] { map.get(num2), i}; | ||
| } | ||
| map.put(nums[i], i) | ||
| } | ||
| } | ||
| } |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 한 번의 순회와 해시셋 저장으로 중복 여부를 결정합니다.
개선 제안: 현재 구현이 적절해 보입니다.