您好,登錄后才能下訂單哦!
這篇文章主要介紹C++實現LeetCode之最短子數組求和的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.
Example:
Input: s = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: the subarray [4,3] has the minimal length under the problem constraint.
Follow up:
If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
這道題給定了我們一個數字,讓求子數組之和大于等于給定值的最小長度,注意這里是大于等于,不是等于。跟之前那道 Maximum Subarray 有些類似,并且題目中要求實現 O(n) 和 O(nlgn) 兩種解法,那么先來看 O(n) 的解法,需要定義兩個指針 left 和 right,分別記錄子數組的左右的邊界位置,然后讓 right 向右移,直到子數組和大于等于給定值或者 right 達到數組末尾,此時更新最短距離,并且將 left 像右移一位,然后再 sum 中減去移去的值,然后重復上面的步驟,直到 right 到達末尾,且 left 到達臨界位置,即要么到達邊界,要么再往右移動,和就會小于給定值。代碼如下:
解法一:
// O(n) class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { if (nums.empty()) return 0; int left = 0, right = 0, sum = 0, len = nums.size(), res = len + 1; while (right < len) { while (sum < s && right < len) { sum += nums[right++]; } while (sum >= s) { res = min(res, right - left); sum -= nums[left++]; } } return res == len + 1 ? 0 : res; } };
同樣的思路,我們也可以換一種寫法,參考代碼如下:
解法二:
class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { int res = INT_MAX, left = 0, sum = 0; for (int i = 0; i < nums.size(); ++i) { sum += nums[i]; while (left <= i && sum >= s) { res = min(res, i - left + 1); sum -= nums[left++]; } } return res == INT_MAX ? 0 : res; } };
下面再來看看 O(nlgn) 的解法,這個解法要用到二分查找法,思路是,建立一個比原數組長一位的 sums 數組,其中 sums[i] 表示 nums 數組中 [0, i - 1] 的和,然后對于 sums 中每一個值 sums[i],用二分查找法找到子數組的右邊界位置,使該子數組之和大于 sums[i] + s,然后更新最短長度的距離即可。代碼如下:
解法三:
// O(nlgn) class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { int len = nums.size(), sums[len + 1] = {0}, res = len + 1; for (int i = 1; i < len + 1; ++i) sums[i] = sums[i - 1] + nums[i - 1]; for (int i = 0; i < len + 1; ++i) { int right = searchRight(i + 1, len, sums[i] + s, sums); if (right == len + 1) break; if (res > right - i) res = right - i; } return res == len + 1 ? 0 : res; } int searchRight(int left, int right, int key, int sums[]) { while (left <= right) { int mid = (left + right) / 2; if (sums[mid] >= key) right = mid - 1; else left = mid + 1; } return left; } };
我們也可以不用為二分查找法專門寫一個函數,直接嵌套在 for 循環中即可,參加代碼如下:
解法四:
class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { int res = INT_MAX, n = nums.size(); vector<int> sums(n + 1, 0); for (int i = 1; i < n + 1; ++i) sums[i] = sums[i - 1] + nums[i - 1]; for (int i = 0; i < n; ++i) { int left = i + 1, right = n, t = sums[i] + s; while (left <= right) { int mid = left + (right - left) / 2; if (sums[mid] < t) left = mid + 1; else right = mid - 1; } if (left == n + 1) break; res = min(res, left - i); } return res == INT_MAX ? 0 : res; } };
以上是“C++實現LeetCode之最短子數組求和的示例分析”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。