523 Continuous Subarray Sum
Given a list ofnon-negativenumbers and a targetintegerk, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple ofk, that is, sums up to n*k where n is also aninteger.
Example 1:
Example 2:
Note:
The length of the array won't exceed 10,000.
You may assume the sum of all the numbers is in the range of a signed 32-bit integer.
Current Status: Time Limit Exceeded
The Idea: First I compute an integral array. This always use to find any subsum within the array in O(1) time. For example, the sum within subarray(1, 3)
can be represented in the integral array as integral_ar[0] - integral_ar[3]
, or the difference in accumulative summations. This can be generalized as integral_ar[max(0, A-1)] - integral_ar[B]
. After this point, I brute force check every consecutive sequence with a length greater than 2. I also check for the edge case such as when k = 0
, and we have a valid summation array such as [0, 0]
Complexity: O(n^2) time, O(n) space
Some testing
Last updated