[Swift]LeetCode198. 打家劫舍 | House Robber
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9744435.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.
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: [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.
你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。
给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额。
示例 1:
输入: [1,2,3,1] 输出: 4 解释: 偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3)。 偷窃到的最高金额 = 1 + 3 = 4 。
示例 2:
输入: [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 }
8ms
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 }