-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathletcode-problem.java
More file actions
36 lines (36 loc) · 1.23 KB
/
letcode-problem.java
File metadata and controls
36 lines (36 loc) · 1.23 KB
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
class Solution {
public int numOfUnplacedFruits(int[] fruits, int[] baskets) {
int n = baskets.length;
int N = 1;
while(N <= n) N <<= 1;
// Build the segment tree
int[] segTree = new int[N << 1];
for (int i = 0; i < n; i++)
segTree[N + i] = baskets[i];
for (int i = N - 1; i > 0; i--)
segTree[i] = Math.max(segTree[2 * i], segTree[2 * i + 1]);
int count = 0;
for (int i = 0; i < n; ++i) {
int x = fruits[i];
int index = 1; // Start from the root of the segment tree
if (segTree[index] < x) {
count++;
continue;
}
// Query the first valid basket
while (index < N) {
if (segTree[index * 2] >= x)
index *= 2;
else
index = index * 2 + 1;
}
// Mark the found basket as used and update the segment tree
segTree[index] = -1;
while (index > 1) {
index >>= 1;
segTree[index] = Math.max(segTree[2 * index], segTree[2 * index + 1]);
}
}
return count;
}
}