• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode945.使数组唯一的最小增量|MinimumIncrementtoMakeArrayUnique ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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

给定整数数组 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 操作是不能让数组的每个值唯一的。

提示:

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

424ms

 1 class Solution {
 2     func minIncrementForUnique(_ A: [Int]) -> Int {
 3         var dp = [Int](repeating: 0, count: 40001)
 4         for a in A {
 5             dp[a] += 1
 6         }
 7         var ans = 0
 8         for i in 0 ..< 40000 {
 9             let a = dp[i]
10             guard a > 1 else {
11                 continue
12             }
13             dp[i] = 1
14             ans += a-1
15             dp[i+1] += a-1
16         }
17         if dp[40000] > 1 {
18             let a = dp[40000] - 1
19             for i in 1 ... a {
20                 ans += i
21             }
22         }
23         return ans
24     }
25 }

440ms

 1 class Solution {
 2     func minIncrementForUnique(_ A: [Int]) -> Int {
 3         guard A.count > 1 else {
 4             return 0
 5         }
 6         let upperBound = 80001
 7         var freqArray = Array<Int>(repeating: 0, count: upperBound)
 8         for val in A {
 9             freqArray[val] += 1
10         }
11         
12         var ans = 0
13         var numsOfDupTaken = 0
14         for i in 0 ..< upperBound {
15             let count = freqArray[i]
16             if count > 1 {
17                 numsOfDupTaken += count - 1
18                 ans -= i * (count - 1)
19             } else if numsOfDupTaken > 0 && freqArray[i] == 0 {
20                 ans += i
21                 numsOfDupTaken -= 1
22             }
23         }
24         
25         return ans
26     }
27 }

460ms

 1 class Solution {
 2     func minIncrementForUnique(_ A: [Int]) -> Int {
 3         var count = [Int](repeating: 0, count:100000)
 4         for x in A {
 5             count[x] += 1
 6         }
 7         var ans = 0, taken = 0
 8         for x in 0..<100000 {
 9             if (count[x] >= 2) {
10                 taken += count[x] - 1;
11                 ans -= x * (count[x] - 1);
12             }
13             else if (taken > 0 && count[x] == 0) {
14                 taken -= 1;
15                 ans += x;
16             }
17         }
18         return ans;
19     }
20 }

476ms

 1 class Solution {
 2     func minIncrementForUnique(_ A: [Int]) -> Int {
 3         guard A.count > 1 else {
 4             return 0
 5         }
 6         let sortedA = A.sorted()
 7         var result = 0, prev = sortedA[0]
 8         for i in 1..<sortedA.count {
 9             if sortedA[i] <= prev {
10                 prev += 1
11                 result += (prev - sortedA[i])
12             } else {
13                 prev = sortedA[i]
14             }
15         }
16         return result
17     }
18 }

484ms

 1 class Solution {
 2     func minIncrementForUnique(_ A: [Int]) -> Int {
 3         guard A.count > 1 else {
 4             return 0
 5         }
 6         var sortedA = A.sorted()
 7         var counter = 0
 8         for i in 1...sortedA.count - 1 {
 9             if sortedA[i] <= sortedA[i-1] {
10                 counter += sortedA[i-1] + 1 - sortedA[i]
11                 sortedA[i] = sortedA[i-1] + 1
12             }
13         }
14         return counter
15     }
16 }

508ms

 1 class Solution {
 2     func minIncrementForUnique(_ A: [Int]) -> Int {
 3         var arr:[Int] = A.sorted(by:<)
 4         var r:Int = -1
 5         var ret:Int = 0
 6         for i in 0..<arr.count
 7         {
 8             var to:Int = max(r,arr[i])
 9             ret += abs(to - arr[i])
10             r = to + 1
11         }
12         return ret
13     }
14 }

 552ms

 1 class Solution {
 2     func minIncrementForUnique(_ A: [Int]) -> Int {
 3     
 4         var counts = A.reduce(into: [:]) { dict, val in
 5             dict[val, default: 0] += 1
 6         }
 7 
 8         var increments = 0
 9         var nextPossibleSlot = -1
10         for key in counts.keys.sorted() {
11             var keyCount = counts[key]!
12             while keyCount > 1 {
13                 nextPossibleSlot = max(key, nextPossibleSlot) + 1
14                 while counts[nextPossibleSlot] != nil {
15                     nextPossibleSlot += 1
16                 }
17                 //Now nextPossibleSlot is available
18                 counts[nextPossibleSlot] = 1 //not actually necessary
19                 increments += nextPossibleSlot - key
20                 keyCount -= 1
21             }
22         }
23 
24         return increments
25     }
26 }

592ms

 1 class Solution {
 2     func minIncrementForUnique(_ A: [Int]) -> Int {
 3         guard A.count > 0 else { return 0 }
 4         guard A.count > 1 else { return 0 }
 5         
 6         var sortA = A.sorted()
 7         var mapA: [Int: Int] = [:]
 8         for a in A {
 9             mapA[a] = (mapA[a] ?? 0) + 1
10         }
11         var res = 0
12         var emptIter = sortA.first! - 1
13         for (index, a) in sortA.enumerated() {
14             if a > emptIter + 1 {
15                 emptIter = emptIter + 1
16                 break
17             }
18             emptIter = a
19         }
20         if emptIter == sortA.last! {
21             emptIter += 1
22         }
23         
24         //print(sortA, emptIter)
25         var prev = -1
26         for (index, a) in sortA.enumerated() {
27             if prev == a {
28                 if emptIter <= a {
29                     emptIter = a
30                     while mapA[emptIter] != nil {
31                         emptIter += 1
32                     } 
33                 }
34                 res += (emptIter - a)
35                 emptIter += 1
36                 while mapA[emptIter] != nil {
37                     emptIter += 1
38                 } 
39                 //print(res, emptIter)
40             }
41             prev = a
42         }
43         
44         return res
45     }
46 }

608ms

 1 class Solution {
 2     
 3     func minIncrementForUnique(_ arr: [Int]) -> Int {
 4         guard arr.count > 1 else {
 5             return 0
 6         }        
 7 
 8         let arr = arr.sorted()
 9         var nextHighestValue = arr[0] + 1
10         var result = 0
11         for i in 1..<arr.count {
12             let value = arr[i]
13             result += max(nextHighestValue - value, 0)
14             nextHighestValue = max(value, nextHighestValue) + 1
15         }
16 
17         return result
18     }
19 }

612ms

 1 class Solution {
 2     func minIncrementForUnique(_ A: [Int]) -> Int {
 3 
 4         if A.count <= 1 {
 5             return 0
 6         }
 7         let a = A.sorted()
 8         var ind0 = 1
 9         var curMin = a[0]
10         var counter = 0
11         while ind0 < a.count {
12             if a[ind0] <= curMin {
13                 curMin += 1
14                 counter += curMin - a[ind0]
15             } else {
16                 curMin = max(a[ind0], curMin)
17             }
18             ind0 += 1
19         }
20         return counter
21     }
22 }

772ms

 1 class Solution {
 2      func minIncrementForUnique(_ A: [Int]) -> Int {
 3         var dict: [Int:Node] = [:]
 4         var count = 0
 5         
 6         for num in A {
 7             if dict[num] == nil {
 8                 let node = Node(num + 1)
 9                 dict[num] = node
10             } else {
11                 // Duplicate
12                 var nextVal = num
13                 let newNode = Node(nextVal + 1)
14                 while dict[nextVal] != nil {
15                     if let node = dict[nextVal] {
16                         dict[nextVal] = newNode
17                         nextVal = node.val
18                     }
19                 }
20                 dict[nextVal] = newNode
21                 newNode.val = nextVal + 1
22                 count += (nextVal - num)
23             }
24         }
25         
26         return count
27     }
28     
29     class Node {
30         var val: Int
31         init(_ a: Int) {
32             self.val = a
33         }
34     }
35 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[Swift]LeetCode953.验证外星语词典|VerifyinganAlienDictionary发布时间:2022-07-14
下一篇:
[Swift]LeetCode699.掉落的方块|FallingSquares发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap