Leetcode 561. Array Partition I (Easy difficulty C + +)

Source: Internet
Author: User

Title:
Given an array of 2n integers, your task was to group these integers into n pairs of integer, say (A1, B1), (A2, B2), ..., (a N, BN) which makes sum of min (AI, bi) for all I from 1 to n as large as possible.

Example 1:
Input: [1,4,3,2]

Output:4
Explanation:n is 2, and the maximum sum of pairs is 4.
Note:
n is a positive integer, which are in the range of [1, 10000].
All the integers in the array would be in the range of [-10000, 10000].

Ideas:
is to give the 2n number group, 221 groups, so that each group of the smallest of the number and maximum. The problem is actually very interesting, the final thing we want to do is to choose the number is the smallest of two numbers, but to be as large as possible, so that the sum of n numbers will be the largest result. Think of it this way, first of all, the number of numbers sorted by small to large ordered array, from the largest number of provoke, the largest number obviously will not participate in the sum, then it should choose who to go for the last addition and contribute a large number? is obviously the second largest number; by analogy, the third largest should be paired with the fourth-largest number, which means grouping the ordered array 22. The key to the problem becomes the selection of efficient sorting algorithms .
The reasons why such a number should be chosen are strictly demonstrated as follows:

    • Assume for each pair of I,bi >= AI.
    • Define SM = min (a1,b1) + min (a2,b2) + ... + min (an,bn). The biggest SM is the answer to this question. Because bi >= ai,sm = a1 + A2 + ... + an.
    • Define SA = a1 + b1 + a2 + b2 + ... + an + bn. For a given input, the SA is a constant.
    • Define DI = | Ai-bi |. Because bi >= ai,di = bi-ai, bi = ai+di.
    • Define SD = D1 + d2 + ... + DN.
    • So Sa = A1 + (a1 + D1) + A2 + (A2 + D2) + ... + A + (an + di) = 2Sm + Sd, so sm = (SA-SD)/2. To get the largest SM, the given SA is constant and needs to make the SD as small as possible.
    • So the problem is to find the smallest possible pair in the array that makes Di (the distance between AI and bi). Obviously, the sum of these distances of neighboring elements is the smallest

Sorting problem is a cliché problem, I started with the bubble sort, but the last 4 groups of test data did not pass, error timeout, because O (n^2) complexity processing longer sequence is very time-consuming, but the bubble sort as the classic sorting algorithm, should master, the code is as follows:

1 classSolution {2  Public:3     intArraypairsum (vector<int>&nums) {4         inttemp;5          for(intI=1; I<nums.size (); i++)6         {7            for(intj=0; J<nums.size ()-i-1; j + +)8           {9               if(nums[j]>nums[j+1])Ten               { Onetemp =Nums[j]; Anums[j]=nums[j+1]; -nums[j+1]=temp; -               } the           } -         } - sort (Nums.begin (), Nums.end ()); -         intsum=0; +          for(intI=0; I<nums.size (); i=i+2) -sum+=Nums[i]; +        returnsum; A     } at};
View Code

The last error timed out, so I switched to the fast line, the code is as follows

But the best solution should be to use the sort () function in the STL template to pass all the test data directly, and the code is simple. Time complexity O (N*LOGN), which is already the optimal complexity of the sorting algorithm, but whether or not to allow the use of STL in the interview process should ask the interviewer for advice. Use of the sort () function See Http://www.cplusplus.com/reference/algorithm/sort/?kw=sort

The code is as follows:

1 classSolution {2  Public:3     intArraypairsum (vector<int>&nums) {4 sort (Nums.begin (), Nums.end ());5         intsum=0;6          for(intI=0; I<nums.size (); i=i+2)7sum+=Nums[i];8        returnsum;9       Ten     } One};
View Code

Leetcode 561. Array Partition I (Easy difficulty C + +)

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.