One of the interesting problems related to sequence

Source: Internet
Author: User

Little gossip. directly on the question.

(1) Maximum sub-array and

The problem is already toooooooooold.

The original question is: given an array. Ask for a sub-array (a contiguous paragraph), their and max. Some of the details are whether or not to agree on anything, but it doesn't matter. In fact, many books have it as the topic of dynamic planning, I personally do not like to really use it as a DP, so to speak, I think as DP instead imprisoned people's thinking. It's a bit overqualified, and the real DP is much more complicated than that. There are quite a number of methods. Throw away the violence, there is also a lot of understanding.

(1.1) my own yy. In fact, it is the same as (1.2).

Consecutive numbers we can throw the negative numbers at the beginning and the end, then the non-negative numbers are added together and the negative numbers are added together to form this "heap":

Positive or negative ...

This is because we are either in the same heap or are not selected, so can be treated as a number. (This is because the positive heap to choose must be selected, the negative heap to choose must be to "connect" to the next positive number, otherwise choose negative number why?) So we have a look, first select the first positive, assuming that plus a negative number is more than 0, we continue to choose, that is, just to "prefix" is positive, we can continue to select the numbers, the prefix is negative, we threw a re-election.

In fact (1.2) that's the idea.


(1.2) The classical solution End[i] represents the and of the subarray ending in a[i]. Then End[i + 1] = max (End[i] + a[i + 1], A[i + 1]), in fact this step is equivalent to End[i] >= 0 takes end[i] + a[i + 1], otherwise take a[i + 1], the optimal solution must be somewhere in the end. So finally max{end[0..n-1]} for the request.

It's customary to call this thing dynamic planning ... It's all good, anyway. This really satisfies the optimal sub-problem. That End[i + 1] was launched by End[i. But only this solution can affect the understanding of the problem behind.


(1.3) I personally like the solution. Because it embodies the idea of prefixes and. We consider prefix[i] = a[0] + a[1] + ... +a[i], or prefix[i] is a prefix and. Then no matter what a sub-array is the difference of two prefixes.

The details are to define PREFIX[-1] = 0.

Then the sub-array a[i: J] = Prefix[j]-prefix[i-1]. So how do we beg prefix[i]?

Loop I can, prefix[i] = Prefix[i-1] + a[i]. So how to ask (1.1) in the end[i], to end with A[i] The largest, then we just want to use prefix[i]-prefix[j] just fine. and Prefix[j] is the minimum value of prefix[0..i] can be. So we loop I, each time with Prefix[i] minus the smallest value that ever happened ... This is a natural way of thinking. And this idea is widely used ... The feeling despite (1.2) is dynamic planning. But we can't take it too seriously. and ignoring the existence of (1.3).


(1.4) There is only theoretical significance ... Because it is an O (Nlogn) method. Divide and conquer. Splits an array from the middle. Then the largest subarray is either on the left half. Either on the right half, these two parts are solved recursively. The key is the case where the maximum sub-array spans the middle point. So suppose we ask for the largest and the intermediate points across. Let's simply sweep from the middle to the left. See where to add the largest number of consecutive. The same to the right sweep side to see the number of consecutive add to the best. This is because we have determined that it must cross the middle point, so there is a "point of focus." The main part of this is the complexity of O (n). So the complexity of the whole recursive division algorithm is O (NLOGN).

By the way, the optimal algorithm (1.1)-(1.3) is all O (n).


(2) The maximum number of sub-arrays that agree to exchange once and

than (1) is much more difficult, is to agree to exchange two elements, exchange only once. Of course can not exchange, still is to find the maximum subarray and. Suppose the violence enumerates the two elements of the interchange, and then the maximum Subarray and the O (n^3) are obtained. Try to find the normal maximum subarray first and then greedy how to replace the elements and the like. Can find a counter example. The problem actually has an O (n) algorithm, which is a classic DP, assuming it doesn't understand (1.2) or (1.2) by rote, it doesn't work. This is Codility's challenge.

http://blog.csdn.net/caopengcs/article/details/36899787


(3) The sum of the smallest sub-arrays of absolute values

is to ask for a sub-array, and the absolute value is minimal.

This is codility practice ..., this cannot be solved linearly. and rote memorization (1) is useless and cannot be applied.

This requires (1.3) The idea that the sub-array is the difference of two prefixes, then we can find the prefix that is closest to it before each prefix is found. "Closest" contains the largest, and the smallest larger than it, which can be conquered with a set.

STL has a well-known lower_bound function, that is, Logn time to finish this matter, or you will have to write two fork tree ...

Previous code:

You can write to stdout for debugging purposes, e.g.//cout << "This is a debug message" << Endl; #include & Lt;set>int solution (vector<int> &a) {    //write your code in C++11    set<int> has;    Have.insert (0);    int sum = 0, r = 2000000000;    for (int i = 0;r && (i < a.size ()), ++i) {        Set<int>::iterator t = have.lower_bound (sum + = A[i]);        if (t! = Have.end ()) {            r = min (r, *t-sum);        }        if (t! = Have.begin ()) {            r = min (r, Sum-* (--T));        }        Have.insert (sum);    }    return r;}



One of the interesting problems related to sequence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.