1. 程式人生 > >[Swift]LeetCode213. 打家劫舍 II | House Robber II

[Swift]LeetCode213. 打家劫舍 II | 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 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.

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.

你是一個專業的小偷,計劃偷竊沿街的房屋,每間房內都藏有一定的現金。這個地方所有的房屋都圍成一圈,這意味著第一個房屋和最後一個房屋是緊挨著的。同時,相鄰的房屋裝有相互連通的防盜系統,如果兩間相鄰的房屋在同一晚上被小偷闖入,系統會自動報警。

給定一個代表每個房屋存放金額的非負整數陣列,計算你在不觸動警報裝置的情況下,能夠偷竊到的最高金額。

示例 1:

輸入: [2,3,2]
輸出: 3
解釋: 你不能先偷竊 1 號房屋(金額 = 2),然後偷竊 3 號房屋(金額 = 2), 因為他們是相鄰的。

示例 2:

輸入: [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 }

8ms

 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 }