The largest and most contiguous sub-arrays

Source: Internet
Author: User

Title: Enter an array of integers with positive and negative numbers in the array. One or more consecutive integers in an array make up a sub-array. The maximum value for the and of all sub-arrays. Requires a time complexity of O (n).

Problem Solving Ideas:

We try to accumulate each number in the sample array from beginning to end. Initialized to 0. The first step plus the first number 1, at this time and 1. The next step is to add the number 2, and then it becomes-1, the third step plus the number 3. We note that the sum of the previous cumulative and 1, less than 0, if used-1 plus 3, the sum is 2, than 3 itself is also male. That is, the sub-array starting with the first number and the sum of the sub-arrays that begin with the third number. So we don't have to think about the sub-arrays that start with the first number, and the accumulated sum is discarded.

Number of occurrences of 1 in integers from 1 to n

Title: Enter an integer n to find the number of decimal representations of the n integers from 1 to n in 1 occurrences. For example, enter 12, from 1 to 12 these integers contain 1 of the numbers have 1,10,11 and 12, 11 have appeared 5 times.

Problem Solving Ideas:

The solution of improving time efficiency from the digital law can make the interviewer refreshing

We divide all the numbers from 1 to 21345 into two segments, one from 1 to 1345 and the other from 1346 to 21345.

Let's first look at the number of occurrences from 1346 to 21345 in 1. The emergence of 1 is divided into two situations. The first analysis of 1 appears in the highest case. From 1346 to 21345 of the numbers, 1 appeared in 10000-19999 of these 10,000 digits of the million, a total of 10,000.

It is worth noting that the number of occurrences of the million is not 10,000 for all 5-digit numbers. The number of digits is 1, such as input 12345, 1 appears in 10000-12345 million, the number of occurrences is not 10,000 times, but 2,346 times, that is, remove the highest number after the remaining number plus 1 (that is, 2345+1=2346 times).

The next analysis of 1 appears in the four-digit number other than the highest bit. In the example 1346-21345 of the 20,000 digits in the second 4 digits of 1 appear 2000 times, because the highest is 2, we can then divide 1346-21345 into two segments, 1346-11345 and 11346-21345. For each of the remaining 4 digits, select one of them is 1, the remaining three digits can be selected in 0-9 of these 10 digits, so the total number of occurrences is 2*1000=2000 times according to the permutation principle.

As for the number of occurrences from 1 to 1345 in 1, we can use recursion to obtain it. That's why we're dividing 1-21345 into 1-1345 and 1346-21345. Because the maximum of 21345 is removed and becomes 1345, it is convenient for us to adopt the idea of recursion.

The largest and most contiguous sub-arrays

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.