leetcode算法題3:分組,讓每一個組的最小者,相加以後和最大。想知道桶排序是怎麼樣的嗎?

/*
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.ios

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 is in the range of [1, 10000].
All the integers in the array will be in the range of [-10000, 10000].函數

*/spa

int arrayPairSum(int* nums, int numsSize) {code

}排序

題意:兩兩分組,而後取每組的最小值來求和,讓和最大。


辦法1:

* 把數組排序,升序。鄰近的兩個數做爲一組。索引

* i=0開始,取nums[i*2]來相加,i=numsSize/2時結束。相加出來的值就是目標值。rem

* 時間複雜度O(nlogn)。it

辦法2:

* 桶排序io

   以每一個元素的值+10000做爲索引。

   * 須要多大的空間?

      由於已知每一個數的範圍爲[-10000,10000],因此須要20001長度的數組才能絕對容納這些數。

   * 具體如何排序?

      * 初始化長度爲20001的數組v,每一個元素爲
0。

      * 遍歷nums,假設每一個數爲i,以i+10000做爲索引,命中數組中的某一個vi(bucket),使vi++。

* 排序後,如何求和?

   遍歷v,在vi不爲0的狀況下,取一個,放一個,取出來的加至sum。最終sum爲目標值。i-10000爲原數值。

* 時間複雜度O(n),使用空間換時間。


   * 基礎概念

* c提供的快排的函數爲qsort,4個參數,第1參數爲void*表示數組,第2個參數爲元素的個數,第3個參數爲每一個元素的大小,最後一個參數爲比較函數。比較函數爲自定義函數,形式以下:

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_sort

* 異或的辦法能夠做爲」抓一放一」的手段,好比設j=1,每次j^=1,那j就會從1跟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;
}
相關文章
相關標籤/搜索