Skip to content

Commit b0e07ae

Browse files
committed
Added 2 solutions
1 parent 42a6d39 commit b0e07ae

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public int[] kWeakestRows(int[][] mat, int k) {
3+
Map<Integer, Integer> map = new HashMap<>();
4+
for (int i = 0; i < mat.length; i++) {
5+
map.put(i, getLastIndex(mat[i], 0, mat[i].length - 1) + 1);
6+
}
7+
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(new Comparator<Integer>(){
8+
public int compare(Integer o1, Integer o2) {
9+
int c = map.get(o1) - map.get(o2);
10+
if (c != 0) {
11+
return c;
12+
}
13+
return o1 - o2;
14+
}
15+
});
16+
for (int i = 0; i < mat.length; i++) {
17+
pq.add(i);
18+
}
19+
int[] ans = new int[k];
20+
for (int i = 0; i < k; i++) {
21+
ans[i] = pq.poll();
22+
}
23+
return ans;
24+
}
25+
26+
private int getLastIndex(int[] arr, int start, int end) {
27+
int idx = -1;
28+
while (start <= end) {
29+
int mid = (start + end) / 2;
30+
if (arr[mid] == 1) {
31+
idx = Math.max(idx, mid);
32+
start = mid + 1;
33+
}
34+
else {
35+
end = mid - 1;
36+
}
37+
}
38+
return idx;
39+
}
40+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int minSetSize(int[] arr) {
3+
Map<Integer, Integer> map = new HashMap<>();
4+
for (int num : arr) {
5+
map.put(num, map.getOrDefault(num, 0) + 1);
6+
}
7+
PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>(){
8+
public int compare(Integer o1, Integer o2) {
9+
return map.get(o2) - map.get(o1);
10+
}
11+
});
12+
for (Integer key : map.keySet()) {
13+
pq.add(key);
14+
}
15+
int n = arr.length / 2;
16+
int currSize = 0;
17+
int count = 0;
18+
while (currSize < n) {
19+
currSize += map.get(pq.poll());
20+
count++;
21+
}
22+
return count;
23+
}
24+
}

0 commit comments

Comments
 (0)