1. 程式人生 > >(LeetCode) T213. House Robber II

(LeetCode) T213. House Robber II

Problem:

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, 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.


Before showing the solution of this problem,let's have a look at its easy vision.

T198. House Robber

The difference between these two problem is that the houses in  T198 are not arranged in a circle.

Let's see how to solve this problem.

Thought:

We can't rob the money from the houses that are neighbor,.So if we rob the money from one house,the house in front of it should be safe.So we have two situations.

when we go to one house, stl_be is that we haven't robbed the money in it, while stl_af is that we have robbed the money in it.

And when we go to a new house:

1. stl_be = max{stl_be, stl_af} [don't rob this time]

(At this the stl_be and stl_af are for the house before,so stl_be is that we wouldn't rob the money and stl_af is that we have robbed the money in the house before, so we can't rob for this time)

2.stl_af = stl_be + money[now] [rob this time]

(At this time we will rob the money, so stl_af will update to the sum of  stl_be for the house before and the money in the house now)

Update the stl_be and stl_af for every house except the first house,and we will get the newest data at last.

So the answer is max{stl_be, stl_af}

Solve:


And now we can solve the T213 with using the way in T198.

Thought:

In T213, the houses are arranged in a circle,so we can divide it into two part:house[1] to house[n-1] and house[2] to house[n].If there are n houses.

Solve:


相關推薦

(LeetCode) T213. House Robber II

Problem:You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses

LeetCode: 213. House Robber II

題目描述 You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All ho

[LeetCode] 213. House Robber II

題目 You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All hous

[leetcode]213. House Robber II

這裡的陣列的i值得是 到第i家為止的錢 0的話表示一家都還沒有。 public class Solution { public int rob(int[] nums) { if (nums.length == 0) return 0;

動態規劃Leetcode 213 House Robber II

Leetcode 213 House Robber II Problem Description: 偷東西:不能偷相鄰房子的東西,不然會觸發報警,並且房子是圍成一個環狀的,即首尾兩個房子是相鄰的。要求在不被抓的前提下偷到東西的價值最大。 具體的題目資訊: https

Leetcode 213. House Robber II -打家劫舍,每家都有一定數量的錢,多家組成一個圓形,首尾相鄰,不能偷盜相鄰的兩家,求可偷盜的最大金額

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are&

leetCodeHouse Robber II python實現

原題連結 實現原理解析 該題在House Robber的基礎上讓首位連結形成環,那麼即表示第一個和最後一個不能同時被搶,則問題分解為House Robber(nums[0:len(nums)-1])和House Robber(nums[1:

LeetCode 213:House Robber II

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

[leetcode-213]House Robber II(java)

問題描述: Note: This is an extension of House Robber. After robbing those houses on that street, the thief has found himself a new pl

[LeetCode] House Robber II

1、題目 You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place

House Robber II

one tco span ron list tin with first http After robbing those houses on that street, the thief has found himself a new place for his thie

213. House Robber II

edi 現在 true htm mat class html 代碼 pre After robbing those houses on that street, the thief has found himself a new place for his thiever

213 House Robber II 打家劫舍 II

n-1 tco logs emp 數列 ++ 一個 pty cnblogs 註意事項: 這是 打家劫舍 的延伸。在上次盜竊完一條街道之後,竊賊又轉到了一個新的地方,這樣他就不會引起太多註意。這一次,這個地方的所有房屋都圍成一圈。這意味著第一個房子是最後一個是緊挨著的。同時,

213. House Robber II (DP)

i++ ++ div nbsp color style spa res urn 1 class Solution { 2 public int rob(int[] nums) { 3 int n = nums.length; 4

leetcode 198. House Robber (Easy)

返回 一次 lee href tar 最大的 保存 題意 效率 https://leetcode.com/problems/house-robber/ 題意: 一維數組,相加不相鄰的數組,返回最大的結果。 思路: 一開始思路就是DP,用一維數組保存dp[i]保存如果偷第

[leetcode]337. House Robber III

[leetcode]337. House Robber III Analysis fighting!!!—— [每天刷題並不難0.0] The thief has found himself a new place for his thievery again

LeetCode 198.House Robber (打家劫舍)

題目描述: 你是一個專業的小偷,計劃偷竊沿街的房屋。每間房內都藏有一定的現金,影響你偷竊的唯一制約因素就是相鄰的房屋裝有相互連通的防盜系統,如果兩間相鄰的房屋在同一晚上被小偷闖入,系統會自動報警。 給定一個代表每個房屋存放金額的非負整數陣列,計算你在不觸動警報裝置的情況下,能夠偷竊到的最高金

LeetCode 198. House Robber (房屋搶劫)

原題 You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint st

[leetcode]198. House Robber

class Solution { public int rob(int[] nums) { if(nums.length==0)return 0; int[] memo=new int[nums.length+1]; memo[

LeetCode-198. House Robber

0.原題 You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only cons