★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-wcaswjjz-md.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
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.git
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.github
Example 1:數組
Input: [2,3,2] Output: 3 Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), because they are adjacent houses.
Example 2:微信
Input: [1,2,3,1] Output: 4 Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4.
你是一個專業的小偷,計劃偷竊沿街的房屋,每間房內都藏有必定的現金。這個地方全部的房屋都圍成一圈,這意味着第一個房屋和最後一個房屋是緊挨着的。同時,相鄰的房屋裝有相互連通的防盜系統,若是兩間相鄰的房屋在同一夜被小偷闖入,系統會自動報警。this
給定一個表明每一個房屋存放金額的非負整數數組,計算你在不觸動警報裝置的狀況下,可以偷竊到的最高金額。spa
示例 1:code
輸入: [2,3,2] 輸出: 3 解釋: 你不能先偷竊 1 號房屋(金額 = 2),而後偷竊 3 號房屋(金額 = 2), 由於他們是相鄰的。
示例 2:htm
輸入: [1,2,3,1] 輸出: 4 解釋: 你能夠先偷竊 1 號房屋(金額 = 1),而後偷竊 3 號房屋(金額 = 3)。 偷竊到的最高金額 = 1 + 3 = 4 。
8ms
1 class Solution { 2 func rob(_ nums: [Int]) -> Int { 3 guard nums.count != 1 else { 4 return nums[0] 5 } 6 7 return max(helper(nums, 0, nums.count - 2), helper(nums, 1, nums.count - 1)) 8 } 9 10 fileprivate func helper(_ nums: [Int], _ start: Int, _ end: Int) -> Int { 11 if start > end { 12 return 0 13 } 14 15 var prev = 0, current = 0 16 17 for i in start...end { 18 (current, prev) = (max(prev + nums[i], current), current) 19 } 20 21 return current 22 } 23 }
8msblog
1 class Solution { 2 3 func rob(_ nums: [Int]) -> Int { 4 5 if nums.count == 0 { return 0 } 6 if nums.count == 1 { return nums[0] } 7 8 return max(rob_nonCircular(nums, start: 0, end: nums.count - 1), rob_nonCircular(nums, start: 1, end: nums.count)) 9 } 10 11 func rob_nonCircular(_ nums: [Int], start: Int, end: Int) -> Int { 12 var a = nums[start], na = 0 // adjacent, non-adjacent 13 14 for i in (start+1)..<end { 15 let tmp = a 16 a = na + nums[i] 17 na = max(na, tmp) 18 } 19 20 return max(a, na) 21 } 22 }
12ms
1 class Solution { 2 var cache: [String: Int] = [:] 3 func rob(_ nums: [Int]) -> Int { 4 return max(rob(nums, startingAt: 1, endingAt: nums.count-1), rob(nums, startingAt: 0, endingAt: nums.count-2)) 5 } 6 7 func rob(_ nums: [Int], startingAt: Int, endingAt: Int) -> Int { 8 if nums.count == 0 { 9 return 0 10 } 11 12 if nums.count == 1 { 13 return nums[0] 14 } 15 if startingAt == endingAt { 16 return nums[startingAt] 17 } 18 19 if endingAt == startingAt+1 { 20 return max(nums[startingAt], nums[endingAt]) 21 } 22 23 let first: Int 24 if let firstValue = cache[key(forStartingAt: startingAt, endingAt: endingAt-2)] { 25 first = firstValue 26 } else { 27 first = rob(nums, startingAt: startingAt, endingAt: endingAt-2) 28 cache[key(forStartingAt: startingAt, endingAt: endingAt-2)] = first 29 } 30 31 let second: Int 32 if let secondValue = cache[key(forStartingAt: startingAt, endingAt: endingAt-1)] { 33 second = secondValue 34 } else { 35 second = rob(nums, startingAt: startingAt, endingAt: endingAt-1) 36 cache[key(forStartingAt: startingAt, endingAt: endingAt-1)] = second 37 } 38 39 return max(nums[endingAt]+first, second) 40 41 } 42 43 func key(forStartingAt startingAt: Int, endingAt: Int) -> String { 44 return "StartingAt:" + String(startingAt) + "Ending At:" + String(endingAt) 45 } 46 47 }
20ms
1 class Solution { 2 func rob(_ nums: [Int]) -> Int { 3 if nums.isEmpty { 4 return 0 5 }else if nums.count <= 3 { 6 return nums.max()! 7 } 8 9 return max(rob(nums, 0, nums.count-1), rob(nums, 1, nums.count)) 10 } 11 12 func rob(_ nums : [Int], _ start : Int, _ end : Int) -> Int { 13 var currMax = nums[start] 14 var nextMax = max(currMax, nums[start+1]) 15 16 for i in start+2..<end { 17 let tmp = nextMax 18 nextMax = max(currMax + nums[i], tmp) 19 currMax = tmp 20 } 21 return nextMax 22 } 23 }
24ms
1 class Solution { 2 func rob(_ nums: [Int]) -> Int { 3 let n = nums.count 4 if n == 0 { return 0 } 5 if n == 1 { return nums[0] } 6 if n == 2 { return max(nums[0], nums[1]) } 7 8 // 不考慮首尾相連,且偷第一個的狀況 9 var dp1 = [Int](repeating: 0, count: nums.count + 1) 10 // 不考慮首尾相連,且不偷第一個的狀況 11 var dp2 = [Int](repeating: 0, count: nums.count + 1) 12 13 var result = [Int](repeating: 0, count: nums.count + 1) 14 15 dp1[0] = nums[0] 16 dp1[1] = nums[0] 17 for i in 2 ..< nums.count { 18 dp1[i] = max(dp1[i-1], dp1[i-2] + nums[i]) 19 } 20 21 dp2[1] = nums[1] 22 for i in 2 ..< nums.count { 23 dp2[i] = max(dp2[i-1], dp2[i-2] + nums[i]) 24 } 25 26 result[0] = nums[0] 27 for i in 1 ..< nums.count { 28 result[i] = max(dp1[i-1], dp2[i]) 29 } 30 return result[nums.count - 1] 31 32 } 33 }