国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

198. House Robber

2019-11-08 02:20:15
字體:
來源:轉載
供稿:網友

題目

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.

Credits: Special thanks to @ifanchu for adding this problem and creating all test cases. Also thanks to @ts for adding additional test cases.

Subscribe to see which companies asked this question.


思路

搞一個count數組,遍歷記錄對應的值


int getMax(int a,int b){ return a > b?a:b;}class Solution {public: int rob(vector<int>& nums) { int length = nums.size(); if(length == 0) { return 0; } if(length == 1) { return nums[0]; } int count[length] = {0}; count[0] = nums[0]; count[1] = getMax(nums[0],nums[1]); for(int i = 2;i < length;i++) { count[i] = getMax(count[i-2] + nums[i],count[i-1]); } return count[length - 1]; }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高淳县| 都安| 广西| 金堂县| 开平市| 西和县| 灵武市| 濮阳县| 龙门县| 砀山县| 建始县| 那曲县| 信阳市| 雅安市| 宜宾县| 南川市| 高青县| 本溪市| 延庆县| 乐山市| 漳州市| 武宣县| 饶河县| 炎陵县| 阜新| 黑河市| 长岭县| 留坝县| 斗六市| 阳曲县| 朝阳市| 繁峙县| 鄯善县| 汨罗市| 盱眙县| 秦皇岛市| 崇仁县| 珠海市| 阆中市| 汝阳县| 绥宁县|