【LeetCode】945. Minimum Increment to Make Array Unique 解题报告(Python)
作者: 负雪明烛
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:
- 0 <= A.length <= 40000
- 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)的更多相关文章
- 【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. ...
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)
[LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
- 【LeetCode】453. Minimum Moves to Equal Array Elements 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:模拟过程 方法二:求和-n*最小值 方法三: ...
- 【原创】leetCodeOj --- Find Minimum in Rotated Sorted Array II 解题报告
题目地址: https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目内容: Suppose a sort ...
- 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...
随机推荐
- hbase调优
@ 目录 一.phoenix调优 1.建立索引超时,查询超时 2.预分区 hbase shell预分区 phoenix预分区 3.在创建表的时候指定salting. 4.二级索引 建立行键与列值的映射 ...
- c#页面查询、数据显示
page : <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Queryx ...
- dart系列之:HTML的专属领域,除了javascript之外,dart也可以
目录 简介 DOM操作 CSS操作 处理事件 总结 简介 虽然dart可以同时用作客户端和服务器端,但是基本上dart还是用做flutter开发的基本语言而使用的.除了andorid和ios之外,we ...
- 学习java的第六天
一.今日收获 1.开始了学习手册第二章的学习 2.了解了java里的常量与变量以及数据类型,与c语言的内容类似 二.今日难题 1.都是基础知识,没有什么难题 三.明日目标 1.继续学习java学习手册 ...
- linux 挂载本地iso
mount -t iso9660 -o loop /mnt/temp/rhel-server-6.5-i386-dvd.iso /mnt/cdrom -t :设备类型 iso9660是指CD-ROM光 ...
- cordova配置与开发
1.环境配置 1.1.安装ant 从 apache官网 下载ant,安装并配置,将ant.bat所在目录加到path环境变量,如c:\apache-ant\bin\.在cmd中运行以下语句如不报错即可 ...
- mysql锁相关讲解及其应用
一.mysql的锁类型 了解Mysql的表级锁 了解Mysql的行级锁 (1) 共享/排它锁(Shared and Exclusive Locks) 共享锁和排他锁是InnoDB引擎实现的标准行级别锁 ...
- jquery总结和注意事项
1.关于页面元素的引用通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用dom ...
- 手写Mybatis和Spring整合简单版示例窥探Spring的强大扩展能力
Spring 扩展点 **本人博客网站 **IT小神 www.itxiaoshen.com 官网地址****:https://spring.io/projects/spring-framework T ...
- 捷码:重塑DevOps,打造更流畅紧密的开发与服务交付业务链
捷码Gemcoder 1周前如果有机会安排一场行业吐槽大会,熟悉软件开发.交付.服务业务各环节的业内人士,对开发中的各种扯皮.交付反反复复.运维服务中的提心吊胆,往往会有很多深刻的体验和刻骨铭心的案例 ...