8 Lines Java Solution (0ms)


  • 0
    X
    public class Solution {
        public int rob(int[] nums) {
            int sum1 = 0;
            int sum2 = 0;
            for (int num : nums) {
                int temp = sum2;
                sum2 = Math.max(num + sum1, sum2);
                sum1 = temp;
            }
            return sum2;
        }
    }

Log in to reply
 

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