kadanes-algorithm

Maximum subarray sum modulo M

穿精又带淫゛_ 提交于 2019-12-03 03:56:47
问题 Most of us are familiar with the maximum sum subarray problem. I came across a variant of this problem which asks the programmer to output the maximum of all subarray sums modulo some number M. The naive approach to solve this variant would be to find all possible subarray sums (which would be of the order of N^2 where N is the size of the array). Of course, this is not good enough. The question is - how can we do better? Example: Let us consider the following array: 6 6 11 15 12 1 Let M = 13

Maximum subarray sum modulo M

試著忘記壹切 提交于 2019-12-02 16:16:18
Most of us are familiar with the maximum sum subarray problem . I came across a variant of this problem which asks the programmer to output the maximum of all subarray sums modulo some number M. The naive approach to solve this variant would be to find all possible subarray sums (which would be of the order of N^2 where N is the size of the array). Of course, this is not good enough. The question is - how can we do better? Example: Let us consider the following array: 6 6 11 15 12 1 Let M = 13. In this case, subarray 6 6 (or 12 or 6 6 11 15 or 11 15 12) will yield maximum sum ( = 12 ). We can

Maximum product prefix string

大城市里の小女人 提交于 2019-11-30 09:54:40
The following is a demo question from a coding interview site called codility: A prefix of a string S is any leading contiguous part of S. For example, "c" and "cod" are prefixes of the string "codility". For simplicity, we require prefixes to be non-empty. The product of prefix P of string S is the number of occurrences of P multiplied by the length of P. More precisely, if prefix P consists of K characters and P occurs exactly T times in S, then the product equals K * T. For example, S = "abababa" has the following prefixes: "a", whose product equals 1 * 4 = 4, "ab", whose product equals 2 *

Maximum product prefix string

瘦欲@ 提交于 2019-11-29 15:40:24
问题 The following is a demo question from a coding interview site called codility: A prefix of a string S is any leading contiguous part of S. For example, "c" and "cod" are prefixes of the string "codility". For simplicity, we require prefixes to be non-empty. The product of prefix P of string S is the number of occurrences of P multiplied by the length of P. More precisely, if prefix P consists of K characters and P occurs exactly T times in S, then the product equals K * T. For example, S =

Finding minimal absolute sum of a subarray

心不动则不痛 提交于 2019-11-29 06:27:12
There's an array A containing (positive and negative) integers. Find a (contiguous) subarray whose elements' absolute sum is minimal, e.g.: A = [2, -4, 6, -3, 9] |(−4) + 6 + (−3)| = 1 <- minimal absolute sum I've started by implementing a brute-force algorithm which was O(N^2) or O(N^3) , though it produced correct results. But the task specifies: complexity: - expected worst-case time complexity is O(N*log(N)) - expected worst-case space complexity is O(N) After some searching I thought that maybe Kadane's algorithm can be modified to fit this problem but I failed to do it. My question is -