Another typical problem is that for a numeric string array containing negative values [1... n], to find a substring of array [I... j] (0 <= I <= j <= N... j] and Max.
Note the differences between substrings and subsequences. Substrings are several consecutive elements in an exponential group, and subsequences only require that the order of each element be consistent with that in the array, without consecutive requirements. For an array with n elements, it contains 2 ^ n sub-sequences and n (n + 1)/2 sub-strings. If the exhaustive method is used, it takes at least O (N ^ 2) Time to obtain the answer. Jay from Carnegie Mellon University
Kadane provides a linear time algorithm. Let's take a look at how to solve the maximum substring and Problem in linear time.
Two conclusions are required to describe the correctness of the kadane algorithm. First, for Array [1... n], if array [I... j] is the substring that satisfies and is the largest. For any K (I <= k <= J), we have array [I... k] and greater than 0. Because if K exists, the array [I... k] and less than 0, then we have array [k + 1... the sum of J] is greater than array [I... j], which corresponds to our hypothetical array [I... j] is the biggest substring in the array.
Secondly, we can split the array from left to right into several substrings, so that the sum of the elements of the remaining substrings is less than 0 except the last substrings, and for all substrings array [I... j] and any K (I <= k <j), with array [I... k] and greater than 0. In this case, we need to note that the and largest substrings that meet the condition can only be the prefix of the preceding substrings, but cannot span multiple substrings. Let's assume array [p... q], which is the largest substring of array and array [p... q], spanning array [I... j], array [J + 1... k]. According to our grouping method, I <= m <j makes array [I... m] and array [I... the maximum value in J], where J + 1 <= n <K makes array [J + 1... the sum of N] Is array [J + 1... k. Because array [M + 1... J] makes the sum of array [I... J] less than 0. Now we can compare array [I... m] and array [J + 1... n], if array [I... the sum of M] is greater than array [J + 1... n], array [I... m]> array [p... q], no array [J + 1... n]> array [p... q], no matter who is big, we can find... q] and a larger substring, which is in conflict with our assumptions, so the array [p... q] it is impossible to span two substrings. For more substrings to be crossed, the existence and larger non-span substrings can also be proved because the sum of substrings is negative. This conclusion also applies to single-element and maximum special cases.
Based on the above conclusions, we get the execution process of the kadane algorithm, traverse the target array from start to end, divide the array into substrings that meet the preceding conditions, and obtain the maximum prefixes and, then compare the largest prefix of each substring and obtain the final answer. We use array = {−2,
1, −3, 4, −1, 2, 1, −5, 4} is used as an example to describe the algorithm steps. Through traversal, the array can be divided into the following three sub-strings (-2), (1,-3), (4,-1, 1,-5, 4 ), in this case, (-2) is divided into one group. The maximum prefix of each substring is-2, 1, 6, so the maximum substring of the target string is 6.
The following is the implementation code:
int Kadane(const int array[], size_t length, unsigned int& left, unsigned int& right){unsigned int i, cur_left, cur_right;int cur_max, max;cur_max = max = left = right = cur_left = cur_right = 0;for(i = 0; i < length; ++i){cur_max += array[i];if(cur_max > 0){cur_right = i;if(max < cur_max){max = cur_max;left = cur_left;right = cur_right;}}else{cur_max = 0;cur_left = cur_right = i + 1;}}return max;}
Here, we need to note that the kadane algorithm cannot give the correct answer to the fact that all array elements are negative because they do not meet the above two conclusions.
This is a digital image problem raised by Ulf grenander in 1977. Jay kadane provided this beautiful solution in 1984. Some problems seem to be simple to solve, but the principle is actually much more complicated than the code.