The maximum, minimum, and absolute value of the sum of successive sub-arrays, the minimum value

Source: Internet
Author: User


#include <iostream> #include <vector> #include <algorithm>using namespace std;//the smallest and/or use of the DP idea , loop through each element in the array, add them together, if it is greater than 0, then//the sum of the current element is cleared to 0, otherwise, and the minimum and comparison, update the minimum and, finally get must be sub-array of minimum and/or time complexity: O (n) space complexity: O (1) int minsum (vector <int> &num) {int min_sum = 0, sum = 0;for (int i=0; i<num.size (); i++) {sum + = num[i];if (sum > 0) sum = 0;if ( Sum < min_sum) min_sum = sum;} if (min_sum = = 0)//array only positive number {min_sum = num[0];for (int i=1; i<num.size (); i++) {if (num[i]<min_sum) min_sum = Num[i];}} return min_sum;} To find the maximum and/or use of the sub-array is the idea of DP, sequentially iterate through each element in the array, add them together, if the sum of less than 0, then//the current element is cleared to 0, otherwise, and the maximum and comparison, update the maximum and, finally get must be the maximum and/or time complexity of the subarray: Maxsum (vector<int> &num) {int max_sum = 0, sum = 0;for (int i=0; i<num.size (); i++) {sum + = num[i];if (Sum < 0 sum = 0;if (Sum > max_sum) max_sum = sum;} if (max_sum = = 0)//array only negative numbers {max_sum = num[0];for (int i=1; i<num.size (); i++) {if (num[i]>max_sum) max_sum = Num[i];}} return max_sum;} Find the minimum value of the sum of the subarray//violence-poor lift//time complexity: O (n^2) spatial complexity: O (1) int minAbsSum1 (vector<Int> &num) {int min_abs_sum = int_max;for (int i=0; i<num.size (); i++) {int cur_sum = 0;for (int j=i; J<num.size ( ); J + +) {cur_sum + = Num[j];if (ABS (Cur_sum) < min_abs_sum) Min_abs_sum = ABS (Cur_sum);}} return min_abs_sum;} Find the minimum value of the sum of the subarray//Calculate all Sum[0-j] 0<= J <n, then sort the array of sum[0-j], then for any i,j segment and equal to: sum[i-j]= Sum[0-j]-sum[0-i];// Set the array sum to store the 0-j of the sub-array and//Because the sum has been sorted, only the minimum value of Sum[z]-sum[z-1],sum[z] (0<=z<sum.size ()) must be found after sorting. Z is the sorted index//If the sum[z] case, Z is the sorted index, then Minabs = ABS (Sum[0-i])//If it is sum[z]-sum[z-1] case, then Minabs = ABS (Sum[i]-sum[j])//time complexity: O (NLOGN) Spatial complexity: O (n) int minAbsSum2 (vector<int> &num) {if (Num.size () ==0) return 0;if (Num.size () ==1) return ABS ( NUM[0]); int min_abs_sum;vector<int> Sum;int cur_sum = 0;for (int i=0; i<num.size (); i++) {cur_sum + = Num[i];if ( Cur_sum = = 0) return 0;sum.push_back (cur_sum);} Sort (Sum.begin (), Sum.end ()), Min_abs_sum = ABS (Sum[0]), for (int i=0; i<sum.size ()-1; i++) {int TEMP1 = ABS (sum[i+1]); int TEMP2 = ABS (Sum[i+1]-sum[i]); cur_sum =(TEMP1&LT;TEMP2) temp1:temp2;if (cur_sum = = 0) return 0;if (cur_sum<min_abs_sum) min_abs_sum = cur_sum;} return min_abs_sum;} Find the maximum da value of the sum of the subarray//violent poor lift//time complexity: O (n^2) spatial complexity: O (1) int maxAbsSum1 (vector<int> &num) {int max_abs_sum = 0;for ( int i=0; I<num.size (); i++) {int cur_sum = 0;for (int j=i; j<num.size (); j + +) {cur_sum + = Num[j];if (ABS (cur_sum) > max_abs_sum) max_abs_sum = a BS (cur_sum);}} return max_abs_sum;} Find the maximum value of the sum of the subarray//Calculate all Sum[0-j] 0<= J <n, then sort the array of sum[0-j], then for any i,j segment and equal to: sum[i-j]= Sum[0-j]-sum[0-i];// Set the array sum to store the 0-j of the sub-array and//Because the sum has been sorted, only the maximum value of sum[sum.size () -1]-sum[0],sum[z] (0<=z<sum.size ()) must be found after sorting. Z is the sorted index//if SUM[Z], Z is the sorted index, then Maxabs = ABS (Sum[0-i])//If it is sum[sum.size () -1]-sum[0] case, then Maxabs = ABS (Sum[i]-sum[j]) Complexity of Time: O (nlogn) spatial complexity: O (n) int maxAbsSum2 (vector<int> &num) {if (Num.size () ==0) return 0;if (Num.size () ==1) Return ABS (Num[0]), int max_abs_sum;vector<int> sum;int cur_sum = 0;for (int i=0; i<num.size (); i++) {cur_sum + = Nu M[i];iF (cur_sum = = 0) return 0;sum.push_back (cur_sum);} Sort (Sum.begin (), Sum.end ()), Max_abs_sum = ABS (Sum[sum.size () -1]-sum[0]); for (int i=0; i<sum.size (); i++) {cur_sum = ABS (Sum[i]); if (cur_sum>max_abs_sum) max_abs_sum = cur_sum;} return max_abs_sum;} int main () {int n;while (cin>>n) {vector<int> num;int number;for (int i=0; i<n; i++) {cin>>number; Num.push_back (number);} cout<< "minsum =" <<minsum (num) <<endl;cout<< "maxsum =" <<maxsum (num) <<endl;cout << "MINABSSUM1 =" <<minabssum1 (num) <<endl;cout<< "minAbsSum2 =" <<minabssum2 (num) < <endl;cout<< "MAXABSSUM1 =" <<maxabssum1 (num) <<endl;cout<< "maxAbsSum2 =" << MAXABSSUM2 (num) <<endl;cout<< "------------------------------------" &LT;&LT;ENDL;} return 0;}


The maximum, minimum, and absolute value of the sum of successive sub-arrays, the minimum value

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.