Here is my codelink. Can anyone help me with a test case? Your task is to maximize reminder modulo M, not sum itself. If M=7 then 6 is better than 14, because 6 gives 6 modulo 7, and 14 gives 0 modulo 7 (and 6 is, obviously, more than 0). Your solution will fail on follo...
As we can see the first cell holds only the first value in the array, the second cell hold the sum of the first two values and the third cell holds the first three values and so on... Back to the sum query we can now calculate any query in O(1) using this array. For example:...
And an alternative solution is to use MergeSort to count, also interesting: https://leetcode.com/discuss/79154/short-%26-simple-o-n-log-n
classSolution {public:/** * @param A an integer array * @return A list of integers includes the index of * the first number and the index of the last number*/vector<int> continuousSubarraySum(vector<int>&A) { vector<int>ret; size_t len=A.size();intcurr = A[0];intsum = A[0...
LintCode "Continuous Subarray Sum II" Flip over your mind: in rotated subarray case, we can simply cut the continuous smallest subarray. classSolution {public:/** * @param A an integer array * @return A list of integers includes the index of...