★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-tjdudfef-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given an array nums
of integers, you can perform operations on the array.git
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
.github
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:app
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:spa
nums
is at most 20000
.nums[i]
is an integer in the range [1, 10000]
.給定一個整數數組 nums
,你能夠對它進行一些操做。code
每次操做中,選擇任意一個 nums[i]
,刪除它並得到 nums[i]
的點數。以後,你必須刪除每一個等於 nums[i] - 1
或 nums[i] + 1
的元素。orm
開始你擁有 0 個點數。返回你能經過這些操做得到的最大點數。htm
示例 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 }
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 }