Leetcode algorithm problem 3: grouping, let each group of the smallest, add after and maximum. Do you want to know what the barrel sort is?

Source: Internet
Author: User

/*
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].

*/

int Arraypairsum (int* nums, int numssize) {

}

Test instructions: 22 Group, then take the minimum value of each group to sum, let and Max. Option 1:

* Sort the array in ascending order. The neighboring two numbers as a group.

* I=0 start, take nums[i*2] to add, I=NUMSSIZE/2 end. The added value is the target value.

* Time complexity O (NLOGN).

Option 2:

* Bucket sort .

The index is the value +10000 for each element.

* How much space is needed?

Because the range of each number is known to be [ -10000,10000], an array of 20001 lengths is required to hold these numbers absolutely.

* How to sort?

* Initialize an array of length 20001 V, each element is
0.

* Traverse Nums, assuming that each number is I, with i+10000 as index, hit an array of one VI (bucket), so vi++.

* After sorting, how to sum?

Traverse V, in the case of VI not 0, take one, put one, take out the addition to sum. The final sum is the target value. i-10000 is the original value.

* Time complexity O (n), the use of space to change time.

* Basic Concept

* The function provided by C is qsort,4 parameters, the 1th parameter is void* to represent the array, the 2nd parameter is the number of elements, the 3rd parameter is the size of each element, and the last argument is the comparison function. The comparison function is a custom function in the following form:

int comp (const void* L, const void* R) {

int LV = * ((int*) L);

int RV = * ((int*) R);

if (Lv > RV) {

return 1;

}

else if (LV < RV) {

return-1;

}

return 0;

}


* Bucket Sorting, is a non-comparative sort, faster than the fast, but also more space consumption. The requirement is that the range of each value can be determined beforehand, keeping the array created to hold all the numbers. Generally, the value of the number (or the value after the operation) as the index of the array, and then according to the index can also be inverse calculation of the original value.

* The structure after the barrel sequencing may be:

650) this.width=650; "src=" Http://ot2wtzcnv.bkt.clouddn.com/jianshubucket_sort.png "alt=" Bucket_sort "title=" " Style= "Border:none;height:auto;"/>

* Different or the way can be used as a "catch one" means, such as set j=1, each time j^=1, that J will change from 1 to 0.

#include <stdio.h>

#include <stdlib.h>


int comp (const void* L, const void* R) {

int LV = * ((int*) L);

int RV = * ((int*) R);

if (Lv > RV) {

return 1;

}

else if (LV < RV) {

return-1;

}

return 0;

}

int Arraypairsum (int* nums, int numssize) {

Qsort (Nums, numssize, sizeof (int), comp);

int i=0;

int sum = 0;

while (I < numssize>>1) {

Sum + = nums[i++*2];

}

return sum;

}


int main (int argc, char *argv[])

{

int arr[] = {1, 4, 3, 2};

printf ("%d\n", Arraypairsum (arr, sizeof arr/sizeof *arr));

return 0;

}


#include <iostream>

#include <vector>

using namespace Std;


Class Solution {

Public

int Arraypairsum (vector<int>& nums) {

Vector<int> buckets (20001, 0);

for (auto i:nums) {

BUCKETS[I+10000] + +;

}

int sum = 0;

for (int i = 0, j=1; i < 20001;) {

if (Buckets[i] > 0) {

if (j==1) {

sum + = i-10000;

}

J^=1;

buckets[i]--;

}

else {

i + +;

}

}

return sum;

}


int arrayPairSum2 (vector<int>& nums) {

Vector<int> buckets (20001, 0);

for (auto i:nums) {

BUCKETS[I+10000] + +;

}

int sum = 0;

for (int i = 0, j=1; i < 20001; i++) {

while (Buckets[i] > 0) {

if (j==1) {

sum + = i-10000;

}

J^=1;

buckets[i]--;

}

}

return sum;

}

};


int main (int argc, const char *argv[])

{

Solution so;

int arr[] = {1,4,3,2};

Vector<int> V (arr, arr+sizeof arr/sizeof *arr);

cout << so.arraypairsum2 (v) << Endl;

return 0;

}


Leetcode algorithm problem 3: grouping, let each group of the smallest, add after and maximum. Do you want to know what the barrel sort is?

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.