LeetCode 945. Minimum Increment to Make Array Unique

945. Minimum Increment to Make Array Unique (使数组唯一的最小增量)

链接

https://leetcode-cn.com/problems/minimum-increment-to-make-array-unique

题目

给定整数数组 A,每次 move 操作将会选择任意?A[i],并将其递增?1。

返回使 A?中的每个值都是唯一的最少操作次数。

示例 1:

输入:[1,2,2]
输出:1
解释:经过一次 move 操作,数组将变为 [1, 2, 3]。
示例 2:

输入:[3,2,1,2,1,7]
输出:6
解释:经过 6 次 move 操作,数组将变为 [3, 4, 1, 2, 5, 7]。
可以看出 5 次或 5 次以下的 move 操作是不能让数组的每个值唯一的。
提示:

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

思路

这个我直接用了排序,然后每一个数字若小于等于前一个数,就代表需要增加,增加二者差值加一,然后一个遍历即可求出需要的操作数。

代码

  public int minIncrementForUnique(int[] A) {
    Arrays.sort(A);
    int count = 0;
    for (int i = 1; i < A.length; i++) {
      if (A[i] <= A[i - 1]) {
        count += A[i - 1] - A[i] + 1;
        A[i] = A[i - 1] + 1;
      }
    }
    return count;
  }

原文地址:https://www.cnblogs.com/blogxjc/p/12544252.html

时间: 2024-10-02 17:10:18

LeetCode 945. Minimum Increment to Make Array Unique的相关文章

945. Minimum Increment to Make Array Unique

945. Minimum Increment to Make Array Unique Medium 25111FavoriteShare 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

leetcode -Find Minimum in Rotated Sorted Array II (1)

本人大三狗,大一学物理,大二转专业来了计院.一入计院深似海,从此节操是路人.转眼间一年过去了,基本上课本的知识学的很好,考前突击分数还很光鲜,但是总是觉得空虚.因为在这个讲究技术的年代,没有一点技术压身,是很容易睡不着觉的.近日阅读了不少前人的经验教训,感觉自己的目标很明确,应届入bat,有必要考个研也没问题,保研估计没戏了.在这个讲究实战的年代,我有必要积累一点代码行数了,否则坑定是混不过面试的.而且还自以为是地定制了一批书单,现在看到堆到50cm搞的一堆书,也觉得压力山大.我就是属于这种书看

LeetCode Find Minimum in Rotated Sorted Array II

Find Minimum in Rotated Sorted Array II Total Accepted: 23090 Total Submissions: 73108 My Submissions Question Solution Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexi

LeetCode 462. Minimum Moves to Equal Array Elements II

Given a non-empty integer array, find the minimum number of moves required to make all array elements equal, where a move is incrementing a selected element by 1 or decrementing a selected element by 1. You may assume the array's length is at most 10

Leetcode#153Find Minimum in Rotated Sorted Array

Find Minimum in Rotated Sorted Array Total Accepted: 42341 Total Submissions: 127863My Submissions Question Solution Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the

Leetcode#154Find Minimum in Rotated Sorted Array II

Find Minimum in Rotated Sorted Array II Total Accepted: 25678 Total Submissions: 80978My Submissions Question Solution Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity

LeetCode 453. Minimum Moves to Equal Array Elements C#

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. Example: Input: [1,2,3] Output: 3 Explanation: Only three moves are needed (remem

[leetcode] Find Minimum in Rotated Sorted Array @ Python

source: https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no du

[LeetCode]Find Minimum in Rotated Sorted Array

Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in the array. 这道题是要求找出一个数组中的最小值,但是这个数组是在有序数组的基础上循环右移了K次. 提示可以用二分