198. House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

public class Solution {
    public int rob(int[] num) {
        int len = num.length;
        if(len == 0){
            return 0;
        }else if(len == 1){
            return num[0];
        }
        
        int m[] = new int[len];
        m[0] = num[0];
        m[1] = num[0]<num[1]?num[1]:num[0];
        
        for(int i = 2; i<len; i++){
            m[i] = Math.max(m[i-2] + num[i], m[i-1]);
        }
        return m[len-1];
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,036評論 0 23
  • 題目 You are a professional robber planning to rob houses a...
    miltonsun閱讀 348評論 0 0
  • 問題描述 You are a professional robber planning to rob houses...
    如煙花非花閱讀 441評論 0 0
  • 恰似心動 愈是隱藏,愈是強(qiáng)烈 不敢追求,又不愿將就 最終擦肩而過 已成路人 少年仍在,心卻變 沒有對錯, 只不過我...
    遇子期閱讀 452評論 6 7
  • 276期,感謝1組成員 【日精進(jìn)打卡第81天】 【知~學(xué)習(xí)】 《六項(xiàng)精進(jìn)》讀0遍 共77遍 《六項(xiàng)精進(jìn)》背0遍 共...
    周晨i閱讀 196評論 0 0

友情鏈接更多精彩內(nèi)容