[Swift]LeetCode198. 打家劫舍 | House Robber

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-espiqywq-md.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that 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: [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.

Example 2:微信

Input: [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
             Total amount you can rob = 2 + 9 + 1 = 12.

你是一個專業的小偷,計劃偷竊沿街的房屋。每間房內都藏有必定的現金,影響你偷竊的惟一制約因素就是相鄰的房屋裝有相互連通的防盜系統,若是兩間相鄰的房屋在同一夜被小偷闖入,系統會自動報警。app

給定一個表明每一個房屋存放金額的非負整數數組,計算你在不觸動警報裝置的狀況下,可以偷竊到的最高金額。spa

示例 1:code

輸入: [1,2,3,1]
輸出: 4
解釋: 偷竊 1 號房屋 (金額 = 1) ,而後偷竊 3 號房屋 (金額 = 3)。
     偷竊到的最高金額 = 1 + 3 = 4 。

示例 2:htm

輸入: [2,7,9,3,1]
輸出: 12
解釋: 偷竊 1 號房屋 (金額 = 2), 偷竊 3 號房屋 (金額 = 9),接着偷竊 5 號房屋 (金額 = 1)。
     偷竊到的最高金額 = 2 + 9 + 1 = 12 。

8ms
 1 class Solution {
 2     func rob(_ nums: [Int]) -> Int {
 3         
 4         if nums.count <= 0 {
 5             return 0
 6         }
 7         
 8         var last: Int = 0
 9         var now: Int = 0
10         
11         for i in 0...nums.count - 1 {
12             var temp = last
13             last = now
14             now = max(temp + nums[i],now)
15         }
16         return now
17     }
18 }

8msblog

 1 class Solution {
 2     func rob(_ nums: [Int]) -> Int {
 3     var mem : [Int?] = [Int?](repeating: nil, count: nums.count)
 4     return robHouse(nums, index: 0, mem: &mem)
 5 }
 6 
 7 func robHouse(_ nums: [Int], index : Int, mem : inout [Int?])->Int{
 8     guard index != nums.count else{
 9         return 0
10     }
11     
12     guard index != nums.count - 1 else{
13         if let answer = mem[index]{
14             return answer
15         }else{
16             mem[index] = nums[index]
17             return mem[index]!
18         }
19     }
20     
21     guard index != nums.count - 2 else {
22         if let answer = mem[index]{
23             return answer
24         }else{
25             mem[index] = max(nums[index], nums[index + 1])
26             return mem[index]!
27         }
28     }
29     if let answer = mem[index]{
30         return answer
31     }else{
32         mem[index] = max(nums[index] + robHouse(nums, index: index + 2, mem: &mem), nums[index + 1] + robHouse(nums, index: index + 3, mem: &mem))
33         return mem[index]!
34     }
35     
36 }
37 }

12ms

 1 class Solution {
 2     func rob(_ nums: [Int]) -> Int {
 3         var dp = [Int]()
 4         for _ in 0...nums.count {
 5             dp.append(0)
 6         }
 7         for i in 0..<dp.count {
 8             if i == 0 {
 9                 dp[0] = 0
10                 continue
11             }
12             if i == 1 {
13                 dp[1] = nums[0]
14                 continue
15             }
16             dp[i] = max(dp[i - 2] + nums[i - 1], dp[i - 1])
17         }
18         return dp[nums.count]
19     }
20 }

12ms

 1 class Solution {
 2     func rob(_ nums: [Int]) -> Int {
 3 
 4         var n = nums.count
 5         if n == 0 { return 0 }
 6         if n == 1 { return nums[0] }
 7         var dp = [nums[0], max(nums[0], nums[1])]
 8         for i in 2..<n {
 9              dp.append(max(dp[i-1], dp[i-2]+nums[i]))
10         }
11         return dp.last!
12     }
13 }

16ms

 1 class Solution {
 2     func rob(_ nums: [Int]) -> Int {
 3         var currentMax = 0
 4         var previousMax = 0
 5         var result = 0
 6         
 7         for num in nums {
 8             result = max(currentMax, previousMax + num)
 9             previousMax = currentMax
10             currentMax = result
11         }
12         return result
13     }
14 }
相關文章
相關標籤/搜索