Minimum Size Subarray Sum
Aug 18, 2016
https://leetcode.com/problems/minimum-size-subarray-sum/
Brutal force
Use backtracking to try every possible subarray. O(n!)
complexity.
Can we do better?
Two pointer
Use a end
pointer to indicate the end index for the subarray; initially start
is 0, if the sum of subarray is larger than s
, gradually increase the start
to make the subarray shorter. The end
will traverse through the array giving out every possible subarray.
|
|
Thought about two pointers
Two pointers method is such a beautiful and elegant solution for many questions, however it is hard to come up with.
Basically, max, min subarray problem can be solved by two pointers each points to the start and end subarray.