zoukankan      html  css  js  c++  java
  • [Swift]LeetCode213. 打家劫舍 II | House Robber II

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/10197872.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.

    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 }
  • 相关阅读:
    Linux下支持mysql支持远程ip访问
    vscode显示php函数列表
    摘:关于php调用.net的web service 踩过的坑
    php接收json格式数据(text/xml)
    php查询快递信息
    php获取客户端ip
    PHP到浏览器的缓存机制
    soap缓存问题
    WordPress For SAE 移植
    使用 AWS CloudTrail 记录 IAM 和 AWS STS API 调用
  • 原文地址:https://www.cnblogs.com/strengthen/p/10197872.html
Copyright © 2011-2022 走看看