Skip to content

Commit

Permalink
update 347
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 2, 2024
1 parent caa8a2c commit da7255c
Showing 1 changed file with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public int[] topKFrequent(int[] nums, int k) {
/**
* NOTE !!!
*
* Set PQ with map count in ASCENDING order (e.g. smaller count -> bigger count)
*
* we set PQ as Integer type, (PriorityQueue<Integer>)
* and order PQ by map value (descending order) (x, y) -> map.get(x) - map.get(y)
*
Expand All @@ -62,7 +64,9 @@ public int[] topKFrequent(int[] nums, int k) {
* // );
*
*/
// PQ with custom logic
// PQ with custom logic (increasing order, e.g. order with smaller count val -> bigger count val
// or, we can sort with decreasing order, then we don't need to remove element when pq size > k
// (check below example)
PriorityQueue<Integer> pq = new PriorityQueue<>(
(x, y) -> map.get(x) - map.get(y)
);
Expand All @@ -87,6 +91,47 @@ public int[] topKFrequent(int[] nums, int k) {
return res;
}

// V0-0-1
public int[] topKFrequent_0_1_1(int[] nums, int k) {

Map<Integer, Integer> map = new HashMap<>();
for (Integer x : nums) {
map.put(x, map.getOrDefault(x, 0) + 1);
}

/**
* NOTE !!!
*
* Set PQ with map count in DECREASING order (e.g. bigger count -> smaller count)
*
*/
// PQ with custom logic
PriorityQueue<Integer> pq = new PriorityQueue<>(
(x, y) -> map.get(y) - map.get(x) // NOTE here
);

// NOTE !!! add map element to PQ
for (Integer key : map.keySet()){
pq.add(key);
/**
* NOTE !!! if PQ sort in decreasing order, NO NEED to drop PQ element when size > k
*/
// if (pq.size() > k){
// pq.poll();
// }
}

// pop elements from PQ
int tmp = 0;
int[] res = new int[k];
while (tmp < k) {
res[tmp] = pq.poll();
tmp += 1;
}

return res;
}

// V0-1
// IDEA : HASHMAP + ARRAY ORDERING
public int[] topKFrequent_0_1(int[] nums, int k) {
Expand Down

0 comments on commit da7255c

Please sign in to comment.