Skip to content

Commit e249f1b

Browse files
committed
Time: 98 ms (11.40%), Space: 57.6 MB (88.36%) - LeetHub
1 parent e8b0cc4 commit e249f1b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public int shortestSubarray(int[] nums, int k) {
3+
int ans = Integer.MAX_VALUE;
4+
long Csum=0;
5+
6+
PriorityQueue<Pair> pq = new PriorityQueue<Pair>(
7+
(a, b) -> Long.compare(a.sum, b.sum)
8+
);
9+
10+
for(int i=0;i<nums.length;i++){
11+
Csum += nums[i];
12+
13+
if(Csum>=k){
14+
ans = Math.min(ans , i+1);
15+
}
16+
while(!pq.isEmpty() && Csum-pq.peek().sum >=k){
17+
Pair pp = pq.poll();
18+
ans = Math.min(ans , i-pp.indx);
19+
}
20+
21+
pq.offer(new Pair(Csum , i));
22+
}
23+
return ans == Integer.MAX_VALUE ? -1 : ans;
24+
25+
}
26+
public class Pair{
27+
long sum ;
28+
int indx;
29+
30+
Pair(long _sum,int _indx){
31+
this.sum =_sum;
32+
this.indx= _indx;
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)