No.189 - LeetCode945. Minimum Increment to Make Array Unique - 數組題

每次操作,任意一個元素加1,使所有元素不同。

數組題,由於數據規模較小,直接排序處理即可。

class Solution {
public:
    int minIncrementForUnique(vector<int>& A) {
        int N = A.size();
        if(N <= 1) return 0;
        sort(A.begin(),A.end());
        int ans = 0;
        int now = A[0];
        for(int i=1;i<N;i++){
            if(A[i] > now){
                now = A[i];
            }else{
                ans+=(now - A[i] + 1);
                now++;
            }
        }
        return ans;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章