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

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

213. House Robber II

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

Note: This is an extension of House Robber.

After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the PRevious street.

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[] nums) { if (nums == null || nums.length == 0) return 0; if (nums.length == 1) return nums[0]; return Math.max(helper(nums, 0, nums.length-2), helper(nums, 1, nums.length-1)); } public int helper(int[] nums, int low, int high) { int include = 0, exclude = 0; for (int i = low; i <= high; i++) { int in = include, ex = exclude; include = ex + nums[i]; exclude = Math.max(in, ex); } return Math.max(include, exclude); }}class Solution {public: int rob(vector<int>& nums) { if (nums.empty() || nums.size() == 0) return 0; if (nums.size() == 1) return nums[0]; return max(helper(nums, 0, nums.size()-2), helper(nums, 1, nums.size()-1)); } int helper(vector<int>& nums, int low, int high) { int include = 0, exclude = 0; for (int i = low; i <= high; i++) { int in = include, ex = exclude; include = ex + nums[i]; exclude = max(in, ex); } return max(include, exclude); }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安陆市| 宁南县| 军事| 涿鹿县| 惠州市| 吉木萨尔县| 澄城县| 德化县| 安义县| 乐安县| 嘉荫县| 吴桥县| 焉耆| 宁强县| 读书| 彩票| 兴安县| 霍邱县| 个旧市| 吴忠市| 利津县| 高碑店市| 嘉兴市| 岐山县| 军事| 余姚市| 遂宁市| 桃园市| 镇安县| 临洮县| 南华县| 丁青县| 临沭县| 兴业县| 九龙县| 东宁县| 淮南市| 礼泉县| 施甸县| 沙雅县| 肇源县|