Simplest java solution


  • 33
    W
    public class Solution {
    
        public int rob(int[] num) {
            int last = 0;
            int now = 0;
            int tmp;
            for (int n :num) {
                tmp = now;
                now = Math.max(last + n, now);
                last = tmp;
            }
            return now;        
        }
    }

  • 2
    S

    Please give explanation also.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.