Leetcode of the array only the smallest increment

Title Description

Given an array of integers A, select any operation will move each A [i], and increments its 1.

A return value of each of the minimum number of operations is unique.

Thinking

  1. Violence Solution: front to back through the array using Hash count the number of the current number that appears is not 0 then the digital ++ until the number first appears. Considering the array length value and not more than 40,000, the size of the array be Hash 80,000 (considering the limiting case, all numbers are 40,000)
  2. First sorted and then traversed from beginning to end, if the current before the number == one number count ++, r if the current number <previous number, count + = A [i-1] + 1-A [i], A [i] = A [i-1] +1
  3. Counting Sorting: to iterate, using the number of Hash statistics appear, while the maximum is found in the array. Hash array traversal, if the Hash [i]> 1, then applied to a multi-part [i 1 +] Hash on, while also adding a multi-part count. Finally, do not forget the part Hash [max + 1] of more than one should also move forward

Code

method one:

class Solution {
public:
    int minIncrementForUnique(vector<int>& A) {
        if(A.size() < 2)
            return 0;
        vector<int> Hash(80000);
        int count = 0;
        for(int i = 0; i < A.size();i++)
        {
            if (Hash[A[i]] == 0)
			Hash[A[i]]++;
            else
            {
                int k = A[i];
                while (Hash[k] != 0 && k<80001)
                {
                    count++;
                    k++;
                }
                Hash[k]++;
            }
        }
        return count;
    }
};

Method Two:

class Solution {
public:
    int minIncrementForUnique(vector<int>& A) {
        if(A.size() < 2)
            return 0;
        sort(A.begin(),A.end());
        int count = 0;
        for(int i = 1; i < A.size();i++)
        {
            if(A[i] == A[i-1])
            {
               count++; 
               A[i]++;
            }
            else if(A[i] < A[i-1])
            {
                count += A[i-1] + 1 - A[i];
                A[i] = A[i-1]+1;
            }
        }
        return count;
    }
};

Method three:

class Solution {
public:
    int minIncrementForUnique(vector<int>& A) {
        if(A.size() < 2)
            return 0;
        int res = 0;
        int max = -1;
        vector<int> Hash(40000,0);
        for(int i = 0 ; i < A.size(); i++)
        {
            Hash[A[i]]++;
            if(A[i]>max) max = A[i];
        }
        for(int i = 0; i <= max;i++)
        {
            if(Hash[i] > 1)
            {
                int d = Hash[i] - 1;
                Hash[i+1] += d;
                res += d;
            }
        }
        int d = Hash[max+1]-1;
        res = res + d*(1+d)/2;
        return res;
    }
};
Published 85 original articles · won praise 0 · Views 378

Guess you like

Origin blog.csdn.net/weixin_38312163/article/details/105021780