|
| 1 | +/** |
| 2 | + * Return the length of the shortest, non-empty, contiguous subarray of A with |
| 3 | + * sum at least K. |
| 4 | + * |
| 5 | + * If there is no non-empty subarray with sum at least K, return -1. |
| 6 | + * |
| 7 | + * Example 1: |
| 8 | + * Input: A = [1], K = 1 |
| 9 | + * Output: 1 |
| 10 | + * |
| 11 | + * Example 2: |
| 12 | + * Input: A = [1,2], K = 4 |
| 13 | + * Output: -1 |
| 14 | + * |
| 15 | + * Example 3: |
| 16 | + * Input: A = [2,-1,2], K = 3 |
| 17 | + * Output: 3 |
| 18 | + * |
| 19 | + * Note: |
| 20 | + * 1 <= A.length <= 50000 |
| 21 | + * -10 ^ 5 <= A[i] <= 10 ^ 5 |
| 22 | + * 1 <= K <= 10 ^ 9 |
| 23 | + */ |
| 24 | + |
| 25 | +publicclassShortestSubarrayWithSumAtLeastK862 { |
| 26 | +publicintshortestSubarray(int[]A,intK) { |
| 27 | +intres =Integer.MAX_VALUE; |
| 28 | +intsum =0; |
| 29 | +Map<Integer,Integer>map =newHashMap<>(); |
| 30 | +List<Integer>list =newArrayList<>(); |
| 31 | +list.add(0); |
| 32 | +map.put(0, -1); |
| 33 | +for (inti=0;i<A.length;i++) { |
| 34 | +sum +=A[i]; |
| 35 | +intremain =sum -K; |
| 36 | +intidx =Collections.binarySearch(list,remain); |
| 37 | +if (idx <0)idx = - (idx +1) -1; |
| 38 | +if (idx >=0) { |
| 39 | +intval =list.get(idx); |
| 40 | +if (sum -val >=K &&i -map.get(val) <res) { |
| 41 | +res =i -map.get(val); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | +while (!list.isEmpty() &&list.get(list.size() -1) >=sum) { |
| 46 | +intval =list.remove(list.size()-1); |
| 47 | + } |
| 48 | +list.add(sum); |
| 49 | +map.put(sum,i); |
| 50 | + } |
| 51 | +returnres ==Integer.MAX_VALUE ? -1 :res; |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | +publicintshortestSubarray2(int[]A,intK) { |
| 56 | +intres =Integer.MAX_VALUE; |
| 57 | +intsum =0; |
| 58 | +Map<Integer,Integer>map =newHashMap<>(); |
| 59 | +LinkedList<Integer>list =newLinkedList<>(); |
| 60 | +list.add(0); |
| 61 | +map.put(0, -1); |
| 62 | +for (inti=0;i<A.length;i++) { |
| 63 | +sum +=A[i]; |
| 64 | +intremain =sum -K; |
| 65 | + |
| 66 | +while (!list.isEmpty() &&sum -list.getFirst() >=K) { |
| 67 | +intval =list.removeFirst(); |
| 68 | +if (i -map.get(val) <res) { |
| 69 | +res =i -map.get(val); |
| 70 | + } |
| 71 | +map.remove(val); |
| 72 | + } |
| 73 | + |
| 74 | +while (!list.isEmpty() &&list.getLast() >=sum) { |
| 75 | +intval =list.removeLast(); |
| 76 | +map.remove(val); |
| 77 | + } |
| 78 | +list.add(sum); |
| 79 | +map.put(sum,i); |
| 80 | + } |
| 81 | +returnres ==Integer.MAX_VALUE ? -1 :res; |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | +/** |
| 86 | + * https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/solution/ |
| 87 | + */ |
| 88 | +publicintshortestSubarray3(int[]A,intK) { |
| 89 | +intN =A.length; |
| 90 | +long[]P =newlong[N+1]; |
| 91 | +for (inti =0;i <N; ++i) |
| 92 | +P[i+1] =P[i] + (long)A[i]; |
| 93 | + |
| 94 | +// Want smallest y-x with P[y] - P[x] >= K |
| 95 | +intans =N+1;// N+1 is impossible |
| 96 | +Deque<Integer>monoq =newLinkedList();//opt(y) candidates, as indices of P |
| 97 | + |
| 98 | +for (inty =0;y <P.length; ++y) { |
| 99 | +// Want opt(y) = largest x with P[x] <= P[y] - K; |
| 100 | +while (!monoq.isEmpty() &&P[y] <=P[monoq.getLast()]) |
| 101 | +monoq.removeLast(); |
| 102 | +while (!monoq.isEmpty() &&P[y] >=P[monoq.getFirst()] +K) |
| 103 | +ans =Math.min(ans,y -monoq.removeFirst()); |
| 104 | + |
| 105 | +monoq.addLast(y); |
| 106 | + } |
| 107 | + |
| 108 | +returnans <N+1 ?ans : -1; |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | +} |