Skip to content

Commit 37b0bc0

Browse files
committed
Time: 24 ms (100.00%), Space: 116.3 MB (100.00%) - LeetHub
1 parent fe408cf commit 37b0bc0

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution {
2+
public int minZeroArray(int[] nums, int[][] queries) {
3+
// Sweep Line ALgo :-
4+
int start = 0;
5+
int end = queries.length;
6+
int ans = -1;
7+
8+
while (start <= end) {
9+
int mid = start + (end - start) / 2;
10+
if (isPossible(nums, queries, mid)) {
11+
ans = mid;
12+
end = mid - 1;
13+
} else {
14+
start = mid + 1;
15+
}
16+
}
17+
18+
return ans;
19+
}
20+
21+
public boolean isPossible(int[] nums, int[][] queries,int k){
22+
int n = nums.length;
23+
24+
int[] line = new int[n+1];
25+
26+
for(int idx = 0; idx < k; idx++){
27+
line[queries[idx][0]] += queries[idx][2];
28+
line[queries[idx][1] + 1] -= queries[idx][2];
29+
}
30+
31+
if (line[0] < nums[0]) {
32+
return false;
33+
}
34+
35+
for(int i=1;i<n;i++){
36+
line[i] = line[i]+line[i-1];
37+
38+
if(line[i]<nums[i]){
39+
return false;
40+
}
41+
}
42+
return true;
43+
}
44+
}

0 commit comments

Comments
 (0)