Skip to content

Commit

Permalink
Create 0740-delete-and-earn.go
Browse files Browse the repository at this point in the history
  • Loading branch information
razer96 committed Jan 23, 2023
1 parent c901198 commit 65c3afd
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions go/0740-delete-and-earn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
func deleteAndEarn(nums []int) int {
count := make(map[int]int)

unique := make([]int, 0)

for _, num := range nums {
if _, ok := count[num]; !ok {
unique = append(unique, num)
}

count[num]++
}

sort.Ints(unique)

earn1, earn2 := 0, 0

for i := 0; i < len(unique); i++ {
currEarn := unique[i] * count[unique[i]]

if i > 0 && unique[i] == unique[i - 1] + 1 {
temp := earn2
earn2 = max(earn2, currEarn + earn1)
earn1 = temp
} else {
temp := earn2
earn2 = currEarn + earn2
earn1 = temp
}
}

return earn2
}

func max(a, b int) int {
if a > b {
return a
}

return b
}

0 comments on commit 65c3afd

Please sign in to comment.