-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
85 lines (68 loc) Β· 1.87 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package Heap.prg42626;
public class Main {
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println(sol.solution(new int[]{1, 2, 3, 9, 10, 12}, 7)); // 2
}
}
class Solution {
int n;
int[] heap;
int cursor = 0;
public int solution(int[] scoville, int K) {
n = scoville.length;
heap = new int[(int) Math.pow(2, Math.ceil(Math.log(n) / Math.log(2)) + 1)];
for (int i = 0; i < n; i++) {
insert(scoville[i]);
}
int cnt = 0;
while (cursor > 0 && heap[1] < K) {
insert(removeRoot() + removeRoot() * 2);
cnt ++;
}
if (cursor == 0) return -1;
return cnt;
}
void insert(int value) {
heap[++cursor] = value;
int current = cursor;
int parent = cursor / 2;
while (parent != 0) {
if (heap[parent] > heap[current]) {
swap(current, parent);
current = parent;
parent = current / 2;
} else {
break;
}
}
}
int removeRoot() {
int root = heap[1];
heap[1] = heap[cursor];
heap[cursor--] = 0;
int current = 1;
int child;
while (true) {
int left = current * 2;
if (left > cursor) break;
child = left;
int right = current * 2 + 1;
if (right <= cursor && heap[right] < heap[child]) {
child = right;
}
if (heap[child] < heap[current]) {
swap(current, child);
current = child;
} else {
break;
}
}
return root;
}
void swap(int idx1, int idx2) {
int temp = heap[idx1];
heap[idx1] = heap[idx2];
heap[idx2] = temp;
}
}