LeetCode题解(0945):使数组唯一的最小增量(Python)

题目:原题链接(中等)

标签:数组、排序

解法时间复杂度空间复杂度执行用时
Ans 1 (Python) O ( N l o g N ) O(NlogN) O(NlogN) O ( N ) O(N) O(N)320ms (98.61%)
Ans 2 (Python)
Ans 3 (Python)

解法一:

class Solution:
    def minIncrementForUnique(self, A: List[int]) -> int:
        count = collections.Counter(A)

        ans = 0
        last, waiting = -1, 0
        for now in sorted(count):
            if waiting > 0:
                diff = now - last - 1
                while diff > 0 and waiting > 0:
                    waiting -= 1
                    diff -= 1
                    ans += waiting
            waiting += count[now] - 1
            last = now

            ans += waiting

        while waiting:
            waiting -= 1
            ans += waiting

        return ans
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页