|
50 | 50 |
|
51 | 51 | <!-- 这里可写通用的实现逻辑 -->
|
52 | 52 |
|
| 53 | +**方法一:单调队列** |
| 54 | + |
53 | 55 | <!-- tabs:start -->
|
54 | 56 |
|
55 | 57 | ### **Python3**
|
56 | 58 |
|
57 | 59 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
58 | 60 |
|
59 | 61 | ```python
|
60 |
| - |
| 62 | +class Solution: |
| 63 | + def shortestSubarray(self, nums: List[int], k: int) -> int: |
| 64 | + s = [0] + list(accumulate(nums)) |
| 65 | + ans = float('inf') |
| 66 | + q = deque([0]) |
| 67 | + for i in range(1, len(s)): |
| 68 | + while q and s[i] - s[q[0]] >= k: |
| 69 | + ans = min(ans, i - q.popleft()) |
| 70 | + while q and s[i] <= s[q[-1]]: |
| 71 | + q.pop() |
| 72 | + q.append(i) |
| 73 | + return -1 if ans == float('inf') else ans |
61 | 74 | ```
|
62 | 75 |
|
63 | 76 | ### **Java**
|
64 | 77 |
|
65 | 78 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
66 | 79 |
|
67 | 80 | ```java
|
| 81 | +class Solution { |
| 82 | + public int shortestSubarray(int[] nums, int k) { |
| 83 | + int n = nums.length; |
| 84 | + long[] s = new long[n + 1]; |
| 85 | + for (int i = 0; i < n; ++i) { |
| 86 | + s[i + 1] = s[i] + nums[i]; |
| 87 | + } |
| 88 | + Deque<Integer> q = new ArrayDeque<>(); |
| 89 | + q.offer(0); |
| 90 | + int ans = Integer.MAX_VALUE; |
| 91 | + for (int i = 1; i <= n; ++i) { |
| 92 | + while (!q.isEmpty() && s[i] - s[q.peek()] >= k) { |
| 93 | + ans = Math.min(ans, i - q.poll()); |
| 94 | + } |
| 95 | + while (!q.isEmpty() && s[i] <= s[q.peekLast()]) { |
| 96 | + q.pollLast(); |
| 97 | + } |
| 98 | + q.offer(i); |
| 99 | + } |
| 100 | + return ans == Integer.MAX_VALUE ? -1 : ans; |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +### **C++** |
| 106 | + |
| 107 | +```cpp |
| 108 | +class Solution { |
| 109 | +public: |
| 110 | + int shortestSubarray(vector<int>& nums, int k) { |
| 111 | + int n = nums.size(); |
| 112 | + vector<long long> s(n + 1); |
| 113 | + for (int i = 0; i < n; ++i) s[i + 1] = s[i] + nums[i]; |
| 114 | + deque<int> q{{0}}; |
| 115 | + int ans = INT_MAX; |
| 116 | + for (int i = 1; i <= n; ++i) |
| 117 | + { |
| 118 | + while (!q.empty() && s[i] - s[q.front()] >= k) |
| 119 | + { |
| 120 | + ans = min(ans, i - q.front()); |
| 121 | + q.pop_front(); |
| 122 | + } |
| 123 | + while (!q.empty() && s[i] <= s[q.back()]) q.pop_back(); |
| 124 | + q.push_back(i); |
| 125 | + } |
| 126 | + return ans == INT_MAX ? -1 : ans; |
| 127 | + } |
| 128 | +}; |
| 129 | +``` |
68 | 130 |
|
| 131 | +### **Go** |
| 132 | +
|
| 133 | +```go |
| 134 | +func shortestSubarray(nums []int, k int) int { |
| 135 | + n := len(nums) |
| 136 | + s := make([]int, n+1) |
| 137 | + for i, v := range nums { |
| 138 | + s[i+1] = s[i] + v |
| 139 | + } |
| 140 | + q := []int{0} |
| 141 | + ans := math.MaxInt32 |
| 142 | + for i := 1; i <= n; i++ { |
| 143 | + for len(q) > 0 && s[i]-s[q[0]] >= k { |
| 144 | + ans = min(ans, i-q[0]) |
| 145 | + q = q[1:] |
| 146 | + } |
| 147 | + for len(q) > 0 && s[i] <= s[q[len(q)-1]] { |
| 148 | + q = q[:len(q)-1] |
| 149 | + } |
| 150 | + q = append(q, i) |
| 151 | + } |
| 152 | + if ans == math.MaxInt32 { |
| 153 | + return -1 |
| 154 | + } |
| 155 | + return ans |
| 156 | +} |
| 157 | +
|
| 158 | +func min(a, b int) int { |
| 159 | + if a < b { |
| 160 | + return a |
| 161 | + } |
| 162 | + return b |
| 163 | +} |
69 | 164 | ```
|
70 | 165 |
|
71 | 166 | ### **...**
|
|
0 commit comments