1. 程式人生 > >[Swift]LeetCode740. 刪除與獲得點數 | Delete and Earn

[Swift]LeetCode740. 刪除與獲得點數 | Delete and Earn

turn ring 元素 art then als 兩個 tin 數組

Given an array nums of integers, you can perform operations on the array.

In each operation, you pick any nums[i] and delete it to earn nums[i] points. After, you must delete every element equal to nums[i] - 1 or nums[i] + 1.

You start with 0 points. Return the maximum number of points you can earn by applying such operations.

Example 1:

Input: nums = [3, 4, 2]
Output: 6
Explanation: 
Delete 4 to earn 4 points, consequently 3 is also deleted.
Then, delete 2 to earn 2 points. 6 total points are earned. 

Example 2:

Input: nums = [2, 2, 3, 3, 3, 4]
Output: 9
Explanation: 
Delete 3 to earn 3 points, deleting both 2‘s and the 4.
Then, delete 3 again to earn 3 points, and 3 again to earn 3 points.
9 total points are earned. 

Note:

  • The length of nums is at most 20000.
  • Each element nums[i] is an integer in the range [1, 10000].

給定一個整數數組 nums ,你可以對它進行一些操作。

每次操作中,選擇任意一個 nums[i] ,刪除它並獲得 nums[i] 的點數。之後,你必須刪除每個等於 nums[i] - 1nums[i] + 1 的元素。

開始你擁有 0 個點數。返回你能通過這些操作獲得的最大點數。

示例 1:

輸入: nums = [3, 4, 2]
輸出: 6
解釋: 
刪除 4 來獲得 4 個點數,因此 3 也被刪除。
之後,刪除 2 來獲得 2 個點數。總共獲得 6 個點數。

示例 2:

輸入: nums = [2, 2, 3, 3, 3, 4]
輸出: 9
解釋: 
刪除 3 來獲得 3 個點數,接著要刪除兩個 2 和 4 。
之後,再次刪除 3 獲得 3 個點數,再次刪除 3 獲得 3 個點數。
總共獲得 9 個點數。

註意:

  • nums的長度最大為20000
  • 每個整數nums[i]的大小都在[1, 10000]範圍內。

36ms

 1 class Solution {
 2     func deleteAndEarn(_ nums: [Int]) -> Int {
 3         if nums.count == 0 {
 4             return 0
 5         }
 6         
 7         let maxN = nums.max()!
 8         var M  = [Int](repeating: 0, count: maxN + 1)
 9         var DP = [Int](repeating: 0, count: maxN + 1)
10         
11         for i in 0..<nums.count {
12             M[nums[i]] += nums[i]
13         }
14         
15         DP[0] = M[0]
16         DP[1] = M[1]
17         for i in 2..<M.count {
18             DP[i] = max(DP[i - 2] + M[i],DP[i - 1])
19         }
20         
21         return DP.last!
22     }
23 }

40ms

 1 class Solution {
 2     func deleteAndEarn(_ nums: [Int]) -> Int {
 3         let minValue = nums.min() ?? 0
 4         let maxValue = nums.max() ?? 0
 5         var dp = [Int](repeating: 0, count:maxValue + 1)
 6         for item in nums {
 7             dp[item] += item
 8         }
 9 
10         if maxValue >= 2{
11             for i in 2...maxValue {
12                 dp[i] = max(dp[i-2] + dp[i], dp[i-1])
13             }        
14         }
15 
16         return dp[maxValue]
17     }
18 }

44ms

 1 class Solution {
 2     func deleteAndEarn(_ nums: [Int]) -> Int {
 3         var counts = [Int: Int]()
 4         for num in nums {
 5             counts[num, default: 0] += 1
 6         }
 7         
 8         var prev = -1
 9         var avoid = 0
10         var using = 0
11         
12         for num in counts.keys.sorted() {
13             if num - 1 != prev {
14                 (avoid, using) = (max(avoid, using), num * counts[num]! + max(avoid, using))
15             } else {
16                 (avoid, using) = (max(avoid, using), num * counts[num]! + avoid)
17             }
18             
19             prev = num
20         }        
21         
22         return max(avoid, using)
23     }
24 }

Runtime: 52 ms Memory Usage: 19.3 MB
 1 class Solution {
 2     func deleteAndEarn(_ nums: [Int]) -> Int {
 3         var sums:[Int] = [Int](repeating:0,count:10001)
 4         for num in nums
 5         {
 6             sums[num] += num
 7         }
 8         for i in 2..<10001
 9         {
10             sums[i] = max(sums[i - 1], sums[i - 2] + sums[i])
11         }
12         return sums[10000]
13     }
14 }

92ms

 1 class Solution {
 2     func deleteAndEarn(_ nums: [Int]) -> Int {
 3         if nums.count == 0 {
 4             return 0
 5         }
 6         if nums.count == 1 {
 7             return nums[0]
 8         }
 9         var numCount: [String: Int] = [:]
10         var dp: [String : Int] = [:]
11         var maxCount = 0
12         for i in 0 ..< nums.count {
13             if let a = numCount["\(nums[i])"] {
14                 numCount["\(nums[i])"] = a + 1
15             }else {
16                 numCount["\(nums[i])"] = 1
17             }
18             if nums[i] > maxCount {
19                 maxCount = nums[i]
20             }
21         }
22         dp["0"] = 0
23         if let a = numCount["1"] {
24             dp["1"] = a
25         }else {
26             dp["1"] = 0
27         }
28         for i in 2 ... maxCount {
29             var a = 0
30             if let e = dp["\(i - 1)"] {
31                 a = e
32             }
33             var b = 0
34             var c = 0
35             if let count = dp["\(i - 2)"] {
36                 c = count
37             }
38             if let count = numCount["\(i)"] {
39                 b = count * i + c
40             }
41             dp["\(i)"] = max(a, b)
42         }
43         return dp["\(maxCount)"]!
44     }
45 }

[Swift]LeetCode740. 刪除與獲得點數 | Delete and Earn