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

首頁 > 學院 > 開發(fā)設計 > 正文

198. House Robber

2019-11-08 02:37:29
字體:
供稿:網(wǎng)友

題目

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.


思路

遞歸遍歷就行了,比較簡單


代碼

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]; }};
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 巴青县| 丹棱县| 石楼县| 金乡县| 通江县| 高陵县| 缙云县| 铁力市| 兖州市| 乌拉特中旗| 门头沟区| 平阴县| 铁岭县| 安化县| 阿克| 九寨沟县| 濮阳市| 通榆县| 临夏市| 正蓝旗| 个旧市| 大冶市| 博乐市| SHOW| 高碑店市| 花莲县| 阳原县| 彰武县| 成武县| 鹤壁市| 望谟县| 富裕县| 桃园市| 中方县| 大名县| 昌邑市| 乳山市| 鹿邑县| 汝阳县| 开原市| 青河县|