Skip to content

Commit 65c3afd

Browse files
committed
Create 0740-delete-and-earn.go
1 parent c901198 commit 65c3afd

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

go/0740-delete-and-earn.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
temp := earn2
27+
earn2 = currEarn + earn2
28+
earn1 = temp
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

Comments
 (0)