Algorithmic thinking problem

Source: Internet
Author: User

Solution One: The time complexity is O (n*n), all groups of violence.

Solution Two: Time complexity of O (2*n).

Idea: Array preprocessing, constructing two auxiliary arrays--left interval array, right interval arrays. The maximum value of [0, I] is stored in the left interval array as l[i]. The maximum value of (I, N] is recorded as r[I], and the array is finally traversed.

Solution Three: Time complexity O (N).

Idea: Find the largest number in the array max, minus the difference between the smaller numbers at the end of the array with Max, or the answer if Max subtracts the value of another endpoint with Max at the endpoint. The proof is simple, so that the smallest number alone becomes an interval is the answer.

The main idea: to find out the array of any local minimum value, that is, the minimum point in mathematics is the subscript.

Solving ideas: Two-point solution. First, determine whether the two ends are minimum points, if not, the middle part of the two.

Problem-Solving ideas: (binary tree, the sequence of the method) first by the law fold several times. We found that the number of folds in the nth fold is 2^n-1, and we will find the following rules:

So we found that this is a left node is always down, the right node is always up two fork tree. The result is output in the order of the ordinal line.

Algorithmic thinking problem

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.