Algorithm 1: Exhaustively try all possible
Int maxSubsequenceSum(const int a[], int n){ int i, j, k; int thisSum, maxSum = 0; for (i = 0; i < n; i++) for (j = i; j < n; j++ ) {thisSum = 0; for (k = i; k < j; k++) thisSum += a[k]; if (thisSum > maxSum) maxSum = thisSum; } return maxSum; }
The algorithm complexity is O(n^3) (triple for loop)
Algorithm 2: Improvement of Algorithm One
Int maxSubsequenceSum(const int a[], int n){ int i, j; int thisSum, maxSum = 0; for (i = 0; i < n; i++) { thisSum = 0; for (j = i; j < n; j++) { thisSum += a[j]; if (thisSum > maxSum) maxSum = thisSum; } } return maxSum; }
The algorithm removes unnecessary calculations in Algorithm 1, and the time complexity is O(n^2) (two for loops).
Algorithm 3: divide-and-conquer strategy
Divide control strategy:
Points: Divide the problem into several (usually two) sub-problems of comparable size and then recursively solve them.
Governance: Combining solutions 4 of several problems and possibly doing a little additional work, and finally getting a solution to the entire problem.
In this problem, the largest subsequence may appear in three places: the left half sequence, the right half sequence, and the sequence that traverses the middle to occupy the left and right halves. The first two cases can be solved by recursion. The recursive base case is that the sequence has only one element (left == right). If the element is greater than 0, the element is returned, otherwise it returns 0. The maximum sum of the third case can be obtained by separately finding the sum of the maximum sum of the left part (including the last one of the left half) and the right part (the first one containing the right part), and then adding them.
Int maxSubsequenceSum(const int a[], int left, int right)
{
Int i, mid, maxLeftSum, maxRightSum;
Int maxLeftBorderSum, leftBorderSum;
Int maxRightBorderSum, rightBorderSum;
If (left == right) { /*Base case*/
If (a[left] >= 0)
Return a[left];
Else
Return 0;
}
Mid = left + (right - left) / 2;
maxLeftSum = maxSubsequenceSum(a, left, mid); /*Maximum sum of the left half*/ maxRightSum = maxSubsequenceSum(a, mid+1, right); /*Maximum sum of the right half*/
/* The following finds the maximum sum through the midpoint *//
maxLeftBorderSum = 0, leftBorderSum = 0;
For (i = mid; i >= left; i--)
The middle point of /* and its left-most and */
{
leftBorderSum += a[i];
If (leftBorderSum > maxLeftBorderSum)
maxLeftBorderSum = leftBorderSum;
}
maxRightBorderSum = 0, rightBorderSum = 0;
For (i = mid+1; i <= right; i++) /* The maximum and right of the midpoint to the right
{
rightBorderSum += a[i];
If (rightBorderSum > maxRightBorderSum)
maxRightBorderSum = rightBorderSum;
}
/* Returns the maximum value in three parts */
Return max3(maxLeftSum, maxRightSum, maxLeftBorderSum+maxRightBorderSum);
}
Int max3(int a, int b, int c)
{
Int maxNum = a;
If (b > maxNum)
maxNum = b;
If (c > maxNum)
maxNum = c;
Return maxNum;
}
Take the sequence 2, 4, -1, -5, 4, -1 as an example. The maximum sum of the left half is 2 + 4 = 6; the maximum sum of the right half is 4, the maximum sum across the center is (-1) + 4 + 2) + (-5 + 4) = 0. Therefore, the largest sub-sequence of this sequence is max(6,4,0)=6. Time complexity analysis: Assume that T(n) is the time it takes to solve the largest subsequence of size n and the problem. When n = 1, T(1) = O(1); when n > 1, the total time spent on recursion twice is 2T(n/2), and the time spent by two parallel for loops is O(len (left) + len(right) = O(n), for a total of 2T(n/2)+O(n). In summary, the following equations can be listed:
T(1) = 1T(n) = 2T(n/2) + O(n)
In fact, the above equations are often used in the divide-and-conquer algorithm. From the equations, T(n) = O(nlogn) can be calculated.
Algorithm 4:
Algorithm 3 uses recursion to solve the largest subsequences and problems better. However, careful analysis shows that in the recursive process, the same element is likely to be operated multiple times. Is there a more efficient algorithm? First on the code
Int maxSubsequenceSum(const int a[], int n){ int i; int maxSum, thisSum; maxSum = thisSum = 0; for (i = 0; i < n; i++) { thisSum += a[i]; if ( thisSum > maxSum) maxSum = thisSum; else if (thisSum < 0) thisSum = 0; } return maxSum; }
It can be easily analyzed that the time complexity of the above code is O(n), which is more efficient than the first three. Why is it correct? Intuitively, the if statement of the first for loop guarantees that the maximum sum is stored in maxSum after each update, and we start the scan from i = 0, assuming that i = t (t < n) is scanned, and the maximum at this time And has been saved in maxSum, and the current sum (thisSum) is greater than 0, regardless of the element size when i > t, plus thisSum will always make the sum greater, and if thisSum is less than 0, it will definitely make The sum is smaller, and since it will still be smaller, it will simply come back (thisSum = 0), and some will start to mean something.
One of the attendant advantages of this algorithm is that it only scans the data once. Once a[i] is read and processed, it no longer needs to remember. Therefore, if the array is on disk or tape, it can be read in sequentially, without having to store any part of the array in main memory. Not only that, at any moment, the algorithm can give correct answers to subsequence problems for the data it has read (other algorithms ie the first three do not have this feature). An algorithm with this characteristic is called an online algorithm. An online algorithm that requires only constant space and runs in linear time is a nearly perfect algorithm. ————《Data Structure and Algorithm Analysis》(Chinese version second edition)
A TPU Screen Protector made of the super toughness of the honeycomb structure. Its unique ultra-soft properties allow it to cover the most complex curves and contours in a device.
The self-healing design of the Hydrogel Screen Protector can protect the display screen of the device from damage, leave no air bubbles, and maintain the sensitivity of the touch screen. Advanced anti-fingerprint and dust- and oleophobic overlays keep your screen smudge- and dirt-free. This overlay is also important in providing maximum touch sensitivity for improved high-speed glide and optimal touch response.
The optical transparency of the Hydrogel Film is more than 90%, showing you the most original screen color and bringing the most realistic visual experience.
If you want to know more about the product information of the Hydrogel Screen Protector for Huawei, please click the product details to view the parameters, model, picture, price and other information of the Huawei Screen Protector.
Whether you are a group or an individual, we will do our best to provide you with accurate and comprehensive information about Hydrogel Screen Protectors!
Screen Protector For Huawei,Hydrogel Film for Huawei,TPU Screen Protector for Huawei,Hydrogel Screen Protector for Huawei
Shenzhen Jianjiantong Technology Co., Ltd. , https://www.jjttpucuttingplotter.com