We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a097112 commit b26b1c2Copy full SHA for b26b1c2
kotlin/0740-delete-and-earn.kt
@@ -0,0 +1,25 @@
1
+class Solution {
2
+ fun deleteAndEarn(nums: IntArray): Int {
3
+ val count = HashMap<Int, Int>().apply {
4
+ for (n in nums) this[n] = this.getOrDefault(n, 0) + 1
5
+ }
6
+ val noDups = nums.toSet().toTypedArray().apply { this.sort()}
7
+
8
+ var before = 0
9
+ var sum = 0
10
+ for (i in noDups.indices) {
11
+ val cur = noDups[i] * count[noDups[i]]!!
12
+ if (i > 0 && noDups[i] == noDups[i - 1] + 1) {
13
+ val temp = sum
14
+ sum = maxOf(cur + before, sum)
15
+ before = temp
16
+ } else {
17
18
+ sum = cur + sum
19
20
21
22
23
+ return sum
24
25
+}
0 commit comments