作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/minimum-increment-to-make-array-unique/description/

题目描述

Given an array of integers A, a move consists of choosing any A[i], and incrementing it by 1.

Return the least number of moves to make every value in A unique.

Example 1:

Input: [1,2,2]
Output: 1
Explanation: After 1 move, the array could be [1, 2, 3].

Example 2:

Input: [3,2,1,2,1,7]
Output: 6
Explanation: After 6 moves, the array could be [3, 4, 1, 2, 5, 7].
It can be shown with 5 or less moves that it is impossible for the array to have all unique values.

Note:

  1. 0 <= A.length <= 40000
  2. 0 <= A[i] < 40000

题目大意

每次移动可以把一个数字增加1,现在要把数组变成没有重复数字的数组,问需要的最少移动是多少。

解题方法

暴力求解,TLE

看到这个题有点慌,觉得需要找规律,然后我发现如果这个数字是重复数字,那么需要把它一直不停+1,直到和它不等的数字为止,这个做法非常类似与Hash的一种向后寻找的做法,时间复杂度是O(N^2),果然超时了。

class Solution(object):
def minIncrementForUnique(self, A):
"""
:type A: List[int]
:rtype: int
"""
N = len(A)
seats = [0] * 80010
res = 0
for a in A:
if not seats[a]:
seats[a] = 1
else:
pos = a
while pos < 80010 and seats[pos] == 1:
pos += 1
seats[pos] = 1
res += pos - a
return res

一次遍历

这个思想我觉得还是非常巧妙的,首先先做一个排序。排序之后,使用一个变量保存当前不重复的数字已经增加到哪里了,所以,当下一个数字到来的时候,它应该增加到这个数字的位置,可以直接求出它需要扩大的步数。

class Solution(object):
def minIncrementForUnique(self, A):
"""
:type A: List[int]
:rtype: int
"""
N = len(A)
if N == 0: return 0
A.sort()
res = 0
prev = A[0]
for i in range(1, N):
if A[i] <= prev:
prev += 1
res += prev - A[i]
else:
prev = A[i]
return res

日期

2018 年 11 月 24 日 —— 周日开始!一周就过去了~

【LeetCode】945. Minimum Increment to Make Array Unique 解题报告(Python)的更多相关文章

  1. 【leetcode】945. Minimum Increment to Make Array Unique

    题目如下: Given an array of integers A, a move consists of choosing any A[i], and incrementing it by 1. ...

  2. 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)

    [LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  3. LeetCode 新题: Find Minimum in Rotated Sorted Array II 解题报告-二分法模板解法

    Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...

  4. 112th LeetCode Weekly Contest Minimum Increment to Make Array Unique

    Given an array of integers A, a move consists of choosing any A[i], and incrementing it by 1. Return ...

  5. [Swift]LeetCode945. 使数组唯一的最小增量 | Minimum Increment to Make Array Unique

    Given an array of integers A, a move consists of choosing any A[i], and incrementing it by 1. Return ...

  6. Minimum Increment to Make Array Unique LT945

    Given an array of integers A, a move consists of choosing any A[i], and incrementing it by 1. Return ...

  7. 【LeetCode】453. Minimum Moves to Equal Array Elements 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:模拟过程 方法二:求和-n*最小值 方法三: ...

  8. 【原创】leetCodeOj --- Find Minimum in Rotated Sorted Array II 解题报告

    题目地址: https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目内容: Suppose a sort ...

  9. 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...

随机推荐

  1. python10-高阶函数

    def use_filer(l): """ 获取指定列表/元组中的奇数 :param l: lsit/tuple :return: """ ...

  2. CMSIS-RTOS 信号量Semaphores

    信号量Semaphores 和信号类似,信号量也是一种同步多个线程的方式,简单来讲,信号量就是装有一些令牌的容器.当一个线程在执行过程中,就可能遇到一个系统调用来获取信号量令牌,如果这个信号量包含多个 ...

  3. SpringBoot集成Kafka的实战用法大全

    本文是SpringBoot+Kafka的实战讲解,如果对kafka的架构原理还不了解的读者,建议先看一下<大白话kafka架构原理>.<秒懂kafka HA(高可用)>两篇文章 ...

  4. 数据集成工具—FlinkX

    @ 目录 FlinkX的安装与简单使用 FlinkX的安装 FlinkX的简单使用 读取mysql中student表中数据 FlinkX本地运行 MySQLToHDFS MySQLToHive MyS ...

  5. Oracle中的DBMS_LOCK包的使用

    一.DBMS_LOCK相关知识介绍 锁模式: 名字 描述 数据类型 值 nl_mode Null INTEGER 1 ss_mode Sub Shared: used on an aggregate ...

  6. electron搭建开发环境

    环境:windons10, nodev14.17.1, vscode md a_star cd a_star npm i -g yarn yarn config set ELECTRON_MIRROR ...

  7. [学习总结]6、Android异步消息处理机制完全解析,带你从源码的角度彻底理解

    开始进入正题,我们都知道,Android UI是线程不安全的,如果在子线程中尝试进行UI操作,程序就有可能会崩溃.相信大家在日常的工作当中都会经常遇到这个问题,解决的方案应该也是早已烂熟于心,即创建一 ...

  8. When should we write our own assignment operator in C++?

    The answer is same as Copy Constructor. If a class doesn't contain pointers, then there is no need t ...

  9. Linux学习 - 文件系统常用命令

    一.文件系统查看命令df df [选项] [挂载点] -a 查看所有文件系统信息,包括特殊文件系统 -h 使用习惯单位显示容量 -T 显示文件系统类型 -m 以MB为单位显示容量 -k 以KB为单位显 ...

  10. RAC常见的宏

    1. RAC           作用:用来给某个对象的某个属性绑定信号,只要产生信号内容就会把内容给属性赋值            RAC(_label, text) = _textField.ra ...