198. House Robbers - easy

特别简单题;Linear time, constant space

public class Solution {
    public int rob(int[] nums) {
        if (nums == null || nums.length==0) return 0;
        int n = nums.length;
        if (n == 1) return nums[0];
        if (n == 2) return Math.max(nums[0], nums[1]);
        
        int two_house_before = nums[0], one_house_before = Math.max(nums[0], nums[1]), current_house = 0;
        for (int i=2; i<n; i++) {
            current_house = Math.max(one_house_before, two_house_before + nums[i]);
            two_house_before = one_house_before;
            one_house_before = current_house;
        }
        return current_house;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 那年我刚上小学四年级。小孩子天生爱玩的天性在我身上表现得淋漓尽致,回家一扔下书包,匆忙喝了一大...
    一蓑烟雨任平生L阅读 1,553评论 1 2