国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 美姑县| 泾阳县| 阜阳市| 饶平县| 韶关市| 曲周县| 新乡县| 青海省| 奉新县| 花莲县| 辽阳市| 荔波县| 定兴县| 容城县| 万山特区| 云安县| 兴国县| 曲沃县| 金沙县| 仙居县| 大关县| 嫩江县| 平舆县| 澄城县| 张掖市| 西和县| 贵德县| 酉阳| 灵武市| 耒阳市| 定陶县| 南安市| 西丰县| 邻水| 广河县| 资兴市| 安宁市| 思茅市| 玉门市| 万源市| 石渠县|