Several small algorithm questions about strings: minimum K Number, maximum sum of consecutive sub-arrays, full string arrangement, array cyclic shift

Source: Internet
Author: User

Minimum K count:

Method 1:

With the modified quick sorting, the split function remains unchanged.

The index of the number returned after the split exits if it is equal to k-1 or k,

Greater than k, recursive left

Less than k, the right side of Recursion

The complexity of this method is O (n), but the original data will be moved.

 

Method 2:

With the largest heap with k nodes

If the total number of elements is less than or equal to k, all are returned.

Traverse all elements and add them to a maximum heap in sequence to count the number of elements in the heap.

If the number of elements reaches n = k, when processing the next data d,

If d is greater than or equal to the maximum heap top value, discard it directly.

Otherwise, delete the heap top element and add d to the heap.

Finally, the remaining k elements in the heap are the minimum k

 

This method is complex with O (klogk), but it does not need to move the original data and is suitable for Big Data scenarios.

 

Maximum Sum of continuous subarrays

Problem description: enter an integer array with positive or negative values. One or more consecutive integers form a subarray to calculate the maximum value of the sum of all subarrays.

Solution:

Main Idea: if the sum of the preceding strings is positive, the next number may increase. It's okay to add a negative number, because there may be a positive number with an absolute value greater, resulting in the overall growth.

If the preceding sum is already negative, there is no need to accumulate it. Even if it is followed by a positive number, the previous one will only be slow. So discard

For example, 3-2 3-5 5. After reading the first element, the sum is 3. After adding the second element, the sum is 1, but then the sum of the three elements changes to 4. Then we can see that after-5, the overall change is-1, and there is no need to accumulate it. It will only drag down the preceding sum. Count again, which is 5. Therefore, the final result is 5.

Int sum = INT_MIN; // The initial sum is the minimum 32-bit signed number.
Int curSum = 0; // the current minimum sum
For (int I = 0; I <length; ++ I) // traverses the entire array
{
If (curSum <= 0) // if the current sum is less than 0, it indicates that the count has been dropped and needs to be started again.
CurSum = data [I]; // re-count
Else
CurSum + = data [I]; // continue to accumulate, give the next opportunity!

If (curSum> sum) // determine whether the value is greater
Sum = curSum;
}

Method for string full Arrangement

Recursive solution:

According to the concept of manual calculation, the first element is fixed first, and the subsequent recursive calculations are arranged in full order. The first element has a total of strlen (str) Possibilities and can use loops.

Ideas:

Suppose there are no repeated elements in the sequence. If yes, you only need to make the repeating element only once in the loop.

If (begin = end) // recursive abort Condition
Printf ("% s \ n", str );

For (char * x = begin; x <end; x ++) // Recursion
{
// Exchange the characters pointed to by x and begin
Swap (x, begin );
Permutation (str, begin + 1, end); // Recursive Sequence
// Restore the characters pointed to by x and begin
Swap (x, begin );
}
 

 

Array cyclic shift k

An intuitive way to move the loop to the left: create a buffer of k size, first set 0 ~ Copy the K-1 element, move the following element to the front, and then move the k values in the buffer back to the end of the array

 

Cycle version shifts left (In-Situ Switching version ):

First, we need to reverse the first k elements in the original position, then all the elements in the latter are in the original position, and then the whole array is in the original position.

1234567 cycle shifts three places to the right: 1234567-> 3214567-> 3217654-> 4567123

Shift right of the loop, that is, find the last k elements and the previous elements in the inverse transformation, and then calculate the overall inverse.

 

 

Related Article

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.