Skip to content

[아현] 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

Merged
merged 8 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions contains-duplicate/f-exuan21.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// time : O(n)
// space : O(n)

class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> set = new HashSet<>();
for(int i : nums) {
if(set.contains(i)) {
return true;
}
set.add(i);
}
return false;
}
}

50 changes: 50 additions & 0 deletions kth-smallest-element-in-a-bst/f-exuan21.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {

private int count = 0;
private TreeNode resultNode = null;

public int kthSmallest(TreeNode root, int k) {
searchNode(root, k);
return resultNode.val;
}

public void searchNode(TreeNode node, int k) {

if(resultNode != null) return;

if(node.left != null) {
searchNode(node.left, k);
}

count++;

if(count == k) {
resultNode = node;
return;
}

if(node.right != null) {
searchNode(node.right, k);
}
}
}

// n : 노드 개수
// time : O(n) 최악의 경우 모든 노드를 탐색해야함
// space : O(n) 최악의 경우 한 쪽으로 노드가 치우쳐져 있음
// -> 재귀 호출이 이루어지므로 스택에 쌓임 -> 한 쪽으로 쏠려 있으면 트리의 높이가 n이 됨 (트리의 최대 높이가 스택의 최대 깊이)
16 changes: 16 additions & 0 deletions number-of-1-bits/f-exuan21.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// time : O(1)
// space : O(1)

class Solution {
public int hammingWeight(int n) {
int count = 0;

while(n != 0) {
if((n&1) == 1) count++;
n = n >> 1;
}

return count;
}
}

39 changes: 39 additions & 0 deletions top-k-frequent-elements/f-exuan21.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

class Solution {
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);
Copy link
Contributor

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);

}

PriorityQueue<Map.Entry<Integer, Integer>> queue = new PriorityQueue<>(
(a, b) -> Integer.compare(b.getValue(), a.getValue())
);
Comment on lines +9 to +11
Copy link
Contributor

@taekwon-dev taekwon-dev Aug 17, 2024

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 사용해봐야겠어요! 👍
  • (그리고 훨씬 간결하네요 ㅎㅎ)


for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
queue.offer(entry);
}

int[] res = new int[k];

for(int i = 0; i < k; i++) {
res[i] = queue.poll().getKey();
}

return res;
}
}

// n : nums의 길이
// m : nums에서 서로 다른 숫자의 개수

// time : O(n) + O(m*logm) + O(k*logm) = O(n + m*logm + k*logm)
// 최악의 경우, nums 가 다 unique 하기 때문에 n == m == k 가 됨
// 따라서, O(n*logn)

// space : O(m) + O(m) + O(k) = O(m + k)
// 최악의 경우 n == m == k 가 됨
// 따라서, O(n)