Leetcode#862. Shortest Subarray with Sum at Least K
Problem
Given an integer array nums
and an integer k
, return the length of the shortest non-empty subarray of nums
with a sum of at least k
. If there is no such subarray, return -1
.
A subarray is a contiguous part of an array.
Example 1:
1 | Input: nums = [1], k = 1 |
Example 2:
1 | Input: nums = [1,2], k = 4 |
Example 3:
1 | Input: nums = [2,-1,2], k = 3 |
Constraints:
1 <= nums.length <= 10^5
10^5 <= nums[i] <= 10^5
1 <= k <= 10^9
Solve
想破頭 簡單說就是 找到第一組解後 左右去肢
1 | class Solution: |
暴力解,想當然 Time Limt Exceeded
1 | class Solution: |
error錯誤記錄一
因為題目中有負數
所以不能使用 sliding window
1 | # class Solution: |
本部落格所有文章除特別聲明外,均採用 CC BY-NC-SA 4.0 許可協議。轉載請註明來自 Imisky!
評論