1234567891011121314151617181920212223242526272829 |
- package main
- // func maxInt(x, y int) int {
- // if x > y {
- // return x
- // }
- // return y
- // }
- // DP? try to understand
- func rob(nums []int) int {
- odd, even := 0, 0
- for i := 0; i < len(nums); i++ {
- // if idx is odd
- if i&1 == 1 {
- // rob this store (+nums[i]), or not (even)
- odd = maxInt(odd+nums[i], even)
- } else {
- // rob this store (+nums[i]), or not (odd)
- even = maxInt(even+nums[i], odd)
- }
- }
- return maxInt(odd, even)
- }
- // func main() {
- // arr := []int{1, 2, 435, 6543, 31, 43, 543, 21, 532}
- // fmt.Println(rob(arr))
- // }
|