198.go 554 B

1234567891011121314151617181920212223242526272829
  1. package main
  2. // func maxInt(x, y int) int {
  3. // if x > y {
  4. // return x
  5. // }
  6. // return y
  7. // }
  8. // DP? try to understand
  9. func rob(nums []int) int {
  10. odd, even := 0, 0
  11. for i := 0; i < len(nums); i++ {
  12. // if idx is odd
  13. if i&1 == 1 {
  14. // rob this store (+nums[i]), or not (even)
  15. odd = maxInt(odd+nums[i], even)
  16. } else {
  17. // rob this store (+nums[i]), or not (odd)
  18. even = maxInt(even+nums[i], odd)
  19. }
  20. }
  21. return maxInt(odd, even)
  22. }
  23. // func main() {
  24. // arr := []int{1, 2, 435, 6543, 31, 43, 543, 21, 532}
  25. // fmt.Println(rob(arr))
  26. // }