-
-
Notifications
You must be signed in to change notification settings - Fork 195
[아현] Week01 Solutions #314
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
Conversation
f-exuan21
commented
Aug 12, 2024
•
edited
Loading
edited
- Contains Duplicate
- Number of 1 Bits
- Top K Frequent Elements
- Kth Smallest Element In a Bst
- Palindromic Substrings
안녕하세요, 코치 Helena 입니다. 추가로, 아래 사진과 같이 Projects 에서 iteration 설정도 같이 부탁드립니다! |
contains-duplicate/f-exuan21.java
Outdated
|
||
class Solution { | ||
public boolean containsDuplicate(int[] nums) { | ||
HashSet<Integer> set = new HashSet<>(); |
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<Integer> set = new HashSet<>();
으로 하는게 자바 컨벤션상 더 좋지 않을까 생각이 들었습니다.
public int[] topKFrequent(int[] nums, int k) { | ||
Map<Integer, Integer> map = new HashMap<>(); | ||
for(int num : nums) { | ||
map.put(num, map.getOrDefault(num, 0) + 1); |
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.
(그냥 의견)
merge 함수를 써보시는것도 좋을 수 있을 것 같습니다.
e.g.
map.merge(num, 1, Integer::sum);
|
||
// time : O(n) + O(m log m) + O(k log m) = O(n + m*logm + k*logm) | ||
// | ||
// 최악의 경우 n log n 이 될 수 있음 |
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.
n log n 으로 정리해주시면 되지 않을까 생각됩니다.
저도 1기 때 좀 많이 조언 받은건데
가능하면 문제에서 제시된 문자로만 표현을 해주시는것이 좋습니다.
그런데 여기서 m은 무엇을 나타낸건가요?
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.
m 은 nums 배열에서 unique한 숫자 개수를 표현했습니다.
ex) nums : [1, 1, 1, 2, 2, 3] -> m = 3
최악의 경우 n*logn 이 되기 때문에 주석 수정해서 다시 커밋했습니다.
감사합니다. 👍
PriorityQueue<Map.Entry<Integer, Integer>> queue = new PriorityQueue<>( | ||
(a, b) -> Integer.compare(b.getValue(), a.getValue()) | ||
); |
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.
저는 Map.Entry를 ArrayList를 사용하고 정렬을 했는데, PriorityQueue 사용하니까
- Map.Entry 를 insert 하는데 더 효율적으로 할 수 있겠네요! (다뤄야 하는 Map.Entry가 많을수록 더 유리)
- ArrayList의 insert는 O(N), PrioirtyQueue의 insert는 O(log N)
- 다뤄야 하는 데이터가 많고 정렬해야 할 때 PrioirtyQueue 사용해봐야겠어요! 👍
- (그리고 훨씬 간결하네요 ㅎㅎ)
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.
@f-exuan21 1주차 답안 제출이 얼마 남지 않았는데, 아직 PR이 Draft 상태이네요. 4문제만 푸셔도 충분히 잘 하신 거라고 생각합니다. 가급적 모임 전까지 Draft 해지 및 PR 병합 부탁드리겠습니다.
@f-exuan21 |
number of 1 bits solutions
Top K Frequent Elements Solutions
시간복잡도 공간복잡도 수정
HashSet -> Set 수정
kth-smallest-element-in-a-bst solution
주석 오타 수정