"Classic algorithm" looking for the longest 01 strings (spinster)

Source: Internet
Author: User

These two days in the public number "spinster" see a classic interview algorithm, looking for the longest 01 strings, the original topic is said:

Given an array, the array contains only 0 and 1. Please find one of the longest subsequence, where the number of 0 and 1 is the same.

Example 1:10,101,010 the result is itself.
Example 2:1.101 million results are 110100

This topic, it seems relatively simple, some students may think that the description of the topic in line with the characteristics of dynamic planning, and then began to use dynamic programming solutions, and strive to find the state transfer equation. The feeling of these classmates is very correct. However, to find the state transition equation, we want to transform the original array.

The original is 0 and 1 of the string, we will change 0 to 1. So the topic goal becomes, finds a longest substring, the substring number and is 0. Set the original array to a, dp[i] to represent the sub-array from 0 to I and. The DP iterates through the array again. The array in Example 1 produces the DP as:

0 1 2 3 4 5 6 7
1 0 1 0 1 0 1 0

In this example, the last value is 0, and the length is an even digit. directly satisfies the result.

See Example 2 again:

0 1 2 3 4 5 6
1 2 1 2 1 0 -1

The 5 position is 0, the oldest string starts from 0 to 5, and the length is 6.

In the above two examples, the substring asked is from the beginning, if not from the beginning, what would it be? See this example: 1101100

0 1 2 3 4 5 6
1 2 1 2 3 2 1

By observing the above table, we can get, dp[0]==dp[6]==dp[2],dp[1]==dp[3]. According to DP definition, if dp[i]==dp[j],i a method, we use map to save the value of the DP to the location of the mapping, such as the following table:

DP value Position Maximum position Minimum position Maximum length
1 0,2,6 6 0 6
2 1,3 3 1 2
3 4 4 4 0
Oldest string length 6

Our final algorithm is to consider whether the most common wear is the beginning of the first. The above idea, the time complexity is O (n), spatial complexity is also O (n).

There are other ideas, such as DP saved is [0,i] the number of 1, then dp[j]-dp[i] * 2 = = J-i indicates a[i+1] ... A[J] is a string satisfying the condition, find J-i largest, is the final result, the time complexity of the idea is O (n^2), the spatial complexity of O (n).

"Analysis Complete"

"Classic algorithm" looking for the longest 01 strings (spinster)

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.