国产探花免费观看_亚洲丰满少妇自慰呻吟_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); }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 海伦市| 新田县| 石城县| 荣成市| 新闻| 泸定县| 沙田区| 上饶市| 南宁市| 万山特区| 彩票| 奉节县| 新和县| 文安县| 鹤壁市| 内黄县| 辽中县| 祁连县| 措勤县| 乐平市| 杨浦区| 台中市| 揭西县| 华亭县| 凤凰县| 合肥市| 连南| 喀喇| 安图县| 萍乡市| 通城县| 江津市| 望谟县| 红河县| 库尔勒市| 乐都县| 荆州市| 南丰县| 石狮市| 广灵县| 璧山县|