Proof of feasibility of twosum problem solving algorithm for the first question in Leetcode

Source: Internet
Author: User

Feasibility proof of twosum problem solving algorithm in Leetcode first, introduction

On the first question in Leetcode, there have been many great man-made solutions on the Internet, and a practical algorithm has been put forward for the twosum. I consulted the answers to the blog http://www.zixue7.com/article-9576-1.html when I answered the question. To allow readers to read and understand this article more intuitively, a brief excerpt from the above blog is as follows:

  • Title Restoration
    The Sum of
    Given an array of integers, find the numbers such that they add up to a specific target number.
    The function twosum should return indices of the numbers such that they add up to the target, where index1 must is Les S than Index2. Please note that your returned answers (both Index1 and INDEX2) is not zero-based.
    You may assume this each input would has exactly one solution.
    Input: numbers={2, 7, one, target=9
    Output: index1=1, index2=2
    The Chinese meaning is to give you an array of integers and a target value, find out the array of two elements and the target value of the subscript, the output is index1 and index2, according to the order from small to large output. and assume that each array contains exactly one solution, even if, exactly, each given array has only one conforming result.

  • Answer
    First of all, to do this kind of topic is generally need to sort, in order to speed, but also to facilitate the search.
    Of course, no sort can be done, but that complexity seems a bit unreasonable.
    Although the given array is not given sequentially, we need to know that in the world of this algorithm, in order, everything is simple.
    Order of value AH. Unfortunately, my life is still a little confusing and no chapters ....
    But since the last thing to return is the element subscript of the original array, we have to make a copy of the space using the original array. The following algorithm is used for the resulting copy.

  • algorithm
    The key algorithms are as follows:
    Select the left and right two cursors for the resulting copy, and the sum of the two, if and equal to the target value, the element represented in the original array lookup determines the subscript, and then saved in index. Left to return.
    If and greater than the target value, then the right subscript to the left shift;
    If and less than the target value, move the left subscript to the right.
    Until the left and right are equal.

Ii. question-making

By the implementation of code, the above algorithm is feasible. But is it possible to prove the feasibility from a mathematical point of view? So we put forward the following proof questions:
For an ordered integer sequence, it is known that there are only a pair of numbers (two numbers) and a given value, and the above algorithm can be used to find out the two numbers.

Third, the problem of the solution

Suppose these two are looking for the number p and Q (and p<=q) respectively.
Since the algorithm starts with the leftmost (small) and the most right (large) two numbers as cursors, we try to discuss the following two scenarios:

Situation One:

If the left and right numbers are exactly equal to the given value, the left and right numbers are the two numbers to be found;

Situation Two:

If the left and right numbers are not equal to the given value, move the Zoo to the right, or move the right cursor to the left. Since the uniqueness of the integer is assumed in the title, we just need to prove that we can find it. However , if the left cursor misses p and the right cursor misses the q in the case of the Zoo right and right cursor moving left . One of the things you might miss is that when Zoo does not reach p, the right cursor has moved to a location that is less than Q.
What this blog is trying to prove is that this "miss" scenario does not happen.

We divide the whole sequence into three intervals:
Interval one: [leftmost value, p], contains the leftmost value, does not contain p;
Interval two: [P,q], contains p, also contains q;
Interval three: (Q, right-most value], does not contain q, contains the right-most value.
Then, in the process of Zoo right and right cursor shift left, one of them must first enter the interval two, may be set to Zoo just arrived at p. At this point the right cursor is still in interval three. As a result, the left cursor + right cursor and must exceed the given value, according to the algorithm, the right cursors will move left until the Q is reached.
This proves that the "missed" scenario does not occur.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Proof of feasibility of twosum problem solving algorithm for the first question in Leetcode

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.