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 c901198 commit 65c3afdCopy full SHA for 65c3afd
go/0740-delete-and-earn.go
@@ -0,0 +1,41 @@
1
+func deleteAndEarn(nums []int) int {
2
+ count := make(map[int]int)
3
+
4
+ unique := make([]int, 0)
5
6
+ for _, num := range nums {
7
+ if _, ok := count[num]; !ok {
8
+ unique = append(unique, num)
9
+ }
10
11
+ count[num]++
12
13
14
+ sort.Ints(unique)
15
16
+ earn1, earn2 := 0, 0
17
18
+ for i := 0; i < len(unique); i++ {
19
+ currEarn := unique[i] * count[unique[i]]
20
21
+ if i > 0 && unique[i] == unique[i - 1] + 1 {
22
+ temp := earn2
23
+ earn2 = max(earn2, currEarn + earn1)
24
+ earn1 = temp
25
+ } else {
26
27
+ earn2 = currEarn + earn2
28
29
30
31
32
+ return earn2
33
+}
34
35
+func max(a, b int) int {
36
+ if a > b {
37
+ return a
38
39
40
+ return b
41
0 commit comments