【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 by1.Return the least number of moves to make every value in
Aunique.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 <= 400000 <= A[i] < 40000
解题思路:本题的解法类似贪心算法。首先找出所有重复的值并且按升序排好,对于每个重复的值,都找出最小的并且大于自身以及当前不与其他值重复的值,两者的差值即为move的步数。
代码如下:
class Solution(object):
def minIncrementForUnique(self, A):
"""
:type A: List[int]
:rtype: int
"""
dic = {}
dup = []
for i in A:
if i in dic:
dup.append(i)
else:
dic[i] = 1
dup.sort()
res = 0
current = 0
for i in dup:
current = max(i+1,current)
while True:
if current in dic:
current += 1
continue
dic[current] = 1
res += (current - i)
current += 1
break
return res
【leetcode】945. Minimum Increment to Make Array Unique的更多相关文章
- 【LeetCode】945. Minimum Increment to Make Array Unique 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解,TLE 一次遍历 日期 题目地址:http ...
- 【leetcode】Find Minimum in Rotated Sorted Array I&&II
题目概述: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 ...
- 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告
今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sort ...
- 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现
一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...
- 【leetcode】453. Minimum Moves to Equal Array Elements
problem 453. Minimum Moves to Equal Array Elements 相当于把不等于最小值的数字都减到最小值所需要次数的累加和. solution1: class So ...
- 【LeetCode】Find Minimum in Rotated Sorted Array 在旋转数组中找最小数
Add Date 2014-10-15 Find Minimum in Rotated Sorted Array Suppose a sorted array is rotated at some p ...
- 【LeetCode】453. Minimum Moves to Equal Array Elements 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:模拟过程 方法二:求和-n*最小值 方法三: ...
- 【LeetCode】462. Minimum Moves to Equal Array Elements II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 方法二:直接找中位数 日期 题目地址: ...
- 【leetcode】Find Minimum in Rotated Sorted Array I & II (middle)
1. 无重复 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 ...
随机推荐
- kylin相关问题
1.Kylin踩坑笔记 - 启动Kylin出现Could not find or load main class org.apache.hadoop.hbase.util.GetJavaPropert ...
- Linux系统重要文件
Linux系统重要文件介绍与配置 一网卡配置文件 (一)网卡配置文件参数介绍 文件路径信息:/ec/sysconfig/network-scripts/ifcfg-eth0 文件作用说明:配置网卡地址 ...
- 重写NSString的setter方法
- (void)setName:(NSString *)name { _name = [name copy]; } 就可以了 不需要写成: 第一种: (void)setName:(NSString * ...
- Servlet 第一天
package com.servlet; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet. ...
- 学习React之前你需要知道的的JavaScript基础知识
在我的研讨会期间,更多的材料是关于JavaScript而不是React.其中大部分归结为JavaScript ES6以及功能和语法,但也包括三元运算符,语言中的简写版本,此对象,JavaScript内 ...
- python中对列表元素大小排序(冒泡排序法和选择排序法)
前言:排序(Sorting) 是计算机程序设计中的一种重要操作,它的功能是将一个数据元素(或记录)的任意序列,重新排列成一个关键字有序的序列.本文主要讲述python中经常用的两种排序算法,选择排序法 ...
- Ubuntu 14.04修复grub
1.进livecd 2.sudo fdisk -l 找到安装分区 3.sudo mount /dev/sda? /mnt 挂载安装分区 4.sudo mount --bind /dev /mnt/de ...
- MVC 入门
MVC是什么? MVC是一个框架模式,它用于把应用程序的输入.处理和输出进行强制性的分开.使用MVC应用程序被分成三个核心部件:模型.视图.控制器.它们各自处理自己的任务.最典型的MVC就是JSP+S ...
- CF1241 D Sequence Sorting(离散化+DP)
题意: 给定数组a[n],用两种操作: 1.将数组中所有值为x的数移至开头 2.将数组中所有值为x的数移至末尾 问,经过最少多少次操作,能将数组a[n]变为非递减的有序数列? (1<=n< ...
- 科普:std::sort干了什么
std::sort算是STL中对OIer比较友好的函数了,但你有想过sort是如何保证它的高速且稳定吗? 正文 我们首先来到第一层:sort函数 template<typename _Rando ...