【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 ...
随机推荐
- 在R语言中使用Stringr进行字符串操作
今天来学习下R中字符串处理操作,主要是stringr包中的字符串处理函数的用法. 先导入stringr包,library(stringr),require(stringr),或者stringr::函数 ...
- Perl字符串处理函数用法集锦
Perl字符串处理函数 0.函数名 index 调用语法position=index(string,substring,position); 解说返回子串substring在字符串string中的位置 ...
- WebRTC网页打开摄像头并录制视频
前面我们能打开本地摄像头,并且在网页上看到摄像头的预览图像. 本文我们使用MediaRecorder来录制视频.在网页上播放录制好的视频,并能提供下载功能. html 首先创建一个html界面,放上一 ...
- 日常Java 2021/11/16
获得applet参数 下面的例子演示了如何使用一个Applet响应来设置文件中指定的参数.该Applet显示了-个黑色棋盘图案和第二种颜色.第二种颜色和每一列的大小通过文档中的Applet的参数指定. ...
- Redis | 第11章 服务器的复制《Redis设计与实现》
目录 前言 1. 旧版复制功能的实现 1.1 同步与命令传播 1.2 旧版复制功能的缺陷 2. 新版复制功能的实现 2.1 部分重同步的实现原理 3. PSYNC 命令的实现 4. 复制的详细步骤 4 ...
- openwrt编译ipk包提示缺少feeds.mk文件
问题具体表现如下 这个问题困扰了我两个多星期,总算解决了.解决方案如下: 首先,先应该把配置菜单调好. 我的硬件是7620a,要编译的ipk包为helloworld,所以应该使用 make menuc ...
- oracle(创建数据库对象)
1 --创建数据库 2 --1.SYSDBA系统权限 3 startup:--启动数据库. 4 shutdown:--关闭数据库. 5 alter database[mount]|[open]|[ba ...
- Spring Batch(0)——控制Step执行流程
Conditional Flow in Spring Batch I just announced the new Learn Spring course, focused on the fundam ...
- FindUserByPageServlet
package com.hopetesting.web.servlet;import com.hopetesting.domain.PageBean;import com.hopetesting.do ...
- 用户信息查询系统_daoImpl
package com.hopetesting.dao.impl;import com.hopetesting.dao.UserDao;import com.hopetesting.domain.Us ...