Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.47 KB | None | 0 0
  1. package dero
  2.  
  3. func max(a int, b int) int {
  4.  
  5.     if a > b {
  6.  
  7.         return a
  8.  
  9.     } else {
  10.  
  11.         return b
  12.  
  13.     }
  14. }
  15.  
  16. func Max_sum_of_non_adjacent(slice [] int) int {
  17.  
  18.     prevOne := 0
  19.     prevTwo := 0
  20.     res     := 0
  21.  
  22.     for index,_ := range(slice) {
  23.  
  24.         if index == 0 {
  25.  
  26.             res = slice[0]
  27.  
  28.         } else if index == 1 {
  29.  
  30.             res = max(slice[0],slice[1])
  31.  
  32.         } else {
  33.  
  34.             res = max(prevOne,prevTwo + slice[index])
  35.  
  36.         }
  37.  
  38.         prevTwo = prevOne
  39.         prevOne = res
  40.  
  41.     }
  42.  
  43.     return res
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement