• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode403.青蛙过河|FrogJump

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10311244.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.

Given a list of stones' positions (in units) in sorted ascending order, determine if the frog is able to cross the river by landing on the last stone. Initially, the frog is on the first stone and assume the first jump must be 1 unit.

If the frog's last jump was k units, then its next jump must be either k - 1, k, or k + 1 units. Note that the frog can only jump in the forward direction.

Note:

  • The number of stones is ≥ 2 and is < 1,100.
  • Each stone's position will be a non-negative integer < 231.
  • The first stone's position is always 0. 

Example 1:

[0,1,3,5,6,8,12,17]

There are a total of 8 stones.
The first stone at the 0th unit, second stone at the 1st unit,
third stone at the 3rd unit, and so on...
The last stone at the 17th unit.

Return true. The frog can jump to the last stone by jumping 
1 unit to the 2nd stone, then 2 units to the 3rd stone, then 
2 units to the 4th stone, then 3 units to the 6th stone, 
4 units to the 7th stone, and 5 units to the 8th stone.

Example 2:

[0,1,2,3,4,8,9,11]

Return false. There is no way to jump to the last stone as 
the gap between the 5th and 6th stone is too large.

一只青蛙想要过河。 假定河流被等分为 x 个单元格,并且在每一个单元格内都有可能放有一石子(也有可能没有)。 青蛙可以跳上石头,但是不可以跳入水中。

给定石子的位置列表(用单元格序号升序表示), 请判定青蛙能否成功过河(即能否在最后一步跳至最后一个石子上)。 开始时, 青蛙默认已站在第一个石子上,并可以假定它第一步只能跳跃一个单位(即只能从单元格1跳至单元格2)。

如果青蛙上一步跳跃了 个单位,那么它接下来的跳跃距离只能选择为 k - 1或 k + 1个单位。 另请注意,青蛙只能向前方(终点的方向)跳跃。

请注意:

  • 石子的数量 ≥ 2 且 < 1100;
  • 每一个石子的位置序号都是一个非负整数,且其 < 231;
  • 第一个石子的位置永远是0。

示例 1:

[0,1,3,5,6,8,12,17]

总共有8个石子。
第一个石子处于序号为0的单元格的位置, 第二个石子处于序号为1的单元格的位置,
第三个石子在序号为3的单元格的位置, 以此定义整个数组...
最后一个石子处于序号为17的单元格的位置。

返回 true。即青蛙可以成功过河,按照如下方案跳跃: 
跳1个单位到第2块石子, 然后跳2个单位到第3块石子, 接着 
跳2个单位到第4块石子, 然后跳3个单位到第6块石子, 
跳4个单位到第7块石子, 最后,跳5个单位到第8个石子(即最后一块石子)。

示例 2:

[0,1,2,3,4,8,9,11]

返回 false。青蛙没有办法过河。 
这是因为第5和第6个石子之间的间距太大,没有可选的方案供青蛙跳跃过去。

64ms
 1 class Solution {
 2     func canCross(_ stones: [Int]) -> Bool {
 3         if stones.count == 0 {
 4             return false
 5         }
 6         let n = stones.count
 7         if n == 1 {
 8             return true
 9         }
10         if stones[1] != 1 {
11             return false
12         }
13         if n == 2 {
14             return true
15         }
16         let last = stones.last!
17         var hs = Set<Int>()
18         for i in 0..<n {
19             if i > 3 && stones[i] > stones[i - 1] * 2 {
20                 return false
21             }
22             hs.insert(stones[i])
23         }
24         return canReach(hs, last, 1, 1)
25     }
26 
27     fileprivate func canReach(_ hs: Set<Int>, _ last: Int, _ pos: Int, _ jump: Int) -> Bool {
28         if pos + jump - 1 == last || pos + jump == last || pos + jump + 1 == last {
29             return true
30         }
31         if hs.contains(pos + jump + 1) {
32             if canReach(hs, last, pos + jump + 1, jump + 1) {
33                 return true
34             }
35         }
36         if hs.contains(pos + jump) {
37             if canReach(hs, last, pos + jump, jump) {
38                 return true
39             }
40         }
41         if jump > 1 && hs.contains(pos + jump - 1) {
42             if canReach(hs, last, pos + jump - 1, jump - 1) {
43                 return true
44             }
45         }
46         return false
47     }
48 }

108ms

 1 class Solution {
 2     func canCross(_ stones: [Int]) -> Bool {        
 3         guard stones.count > 1 else {
 4             return true
 5         }
 6         
 7         var memo = [Int: Int]()
 8         
 9         for i in 0..<stones.count {
10             if i > 3 && stones[i] > stones[i - 1] * 2 {
11                 return false
12             }
13             memo[stones[i]] = i
14         }
15         
16         var deadEnd = [[Bool]](repeating:[Bool](repeating:false, count:stones.count + 1), count: stones.count + 1)
17         
18 
19         
20         func jump(index:Int, preStep:Int) -> Bool {
21             if index == stones.count - 1 {
22                 return true
23             }
24             
25             if deadEnd[index][preStep] == true {
26                 return false
27             }
28             
29             if let nIndex = memo[stones[index] + preStep - 1], nIndex > index{
30                 if jump(index:nIndex ,preStep:preStep - 1) {
31                     return true
32                 } else {
33                     deadEnd[nIndex][preStep - 1] = true
34                 }
35             }
36             
37             if let nIndex = memo[stones[index] + preStep], nIndex > index {
38                 if jump(index:nIndex,preStep:preStep) {
39                     return true
40                 } else {
41                     deadEnd[nIndex][preStep] = true
42                 }
43             }
44             
45             if let nIndex = memo[stones[index] + preStep + 1], nIndex > index {
46                 if jump(index:nIndex,preStep:preStep + 1) {
47                     return true
48                 } else {
49                     deadEnd[nIndex][preStep + 1] = true
50                 }
51             }
52             
53             return false
54         }
55         
56         return jump(index:0, preStep: 0)
57     }
58 }

164ms

 1 class Solution {
 2     func canCross(_ stones: [Int]) -> Bool {
 3         var dp = [String : Bool]()
 4         var map = [Int: Int]()
 5         for i in 0..<stones.count {
 6             map[stones[i]] = i
 7         }
 8         return canCross(stones, map, from: 0, jump: 1, &dp)
 9     }
10     
11     func canCross(_ stones: [Int], _ map: [Int: Int], from i: Int, jump k: Int, _ dp: inout [String: Bool]) -> Bool {        
12         let key = "\(i)\(k)"
13         if let res = dp[key] {
14             return res
15         }
16         
17         if i == stones.count-1 {
18             dp[key] = true
19             return true
20         }
21         
22         if k <= 0 { return false }
23 
24         guard let j = map[stones[i] + k] else  {
25             dp[key] = false
26             return false
27         }
28         
29         let res = canCross(stones, map, from: j, jump: k-1, &dp) 
30             || canCross(stones, map, from: j, jump: k, &dp)
31             || canCross(stones, map, from: j, jump: k+1, &dp)
32         dp[key] = res
33         return res
34     }
35 }

256ms

 1 class Solution {
 2     func canCross(_ stones: [Int]) -> Bool {
 3         let set = Set(stones)
 4         var cache = [String: Bool]()
 5         return helper(set, 0, 0, stones.last!, &cache)
 6     }
 7     
 8     func helper(_ set: Set<Int>, _ i: Int, _ lastJump: Int, _ last: Int, _ cache: inout [String: Bool]) -> Bool {
 9         if i == last { return true }
10         if let r = cache["\(i)-\(lastJump)"] { return r }
11         let possibleJumps = Array(max(lastJump-1, 1)...max(lastJump+1, 1))
12         for j in possibleJumps {
13             if set.contains(i+j) {
14                 if helper(set, i+j , j, last, &cache) {
15                     return true
16                 }
17             }
18         }
19         cache["\(i)-\(lastJump)"] = false
20         return false
21     }
22 }

428ms

 1 class Solution {
 2     func canCross(_ stones: [Int]) -> Bool {
 3         //[stone,possibleStep]
 4         if stones.count == 2 {
 5             return stones[1] == 1
 6         }
 7         var dp = Dictionary<Int,Set<Int>>()
 8         dp[0] = Set<Int>()
 9         dp[0]!.insert(1)
10         for i in 1..<stones.count{
11             dp[stones[i]] = Set<Int>()
12         }
13         let stonesLastIndex = stones.count - 1
14         for i in 0...stonesLastIndex {
15             let stone = stones[i]
16             if let steps = dp[stone] as Set<Int>?{
17                 for step in steps {
18                     let reach = step + stone
19                     if reach == stones[stonesLastIndex]{
20                         return true
21                     }
22                     if var newStoneSet = dp[reach]{
23                         newStoneSet.insert(step)
24                         if step - 1 > 0{
25                             newStoneSet.insert(step - 1)
26                         }
27                         newStoneSet.insert(step + 1)
28                         dp[reach] = newStoneSet
29 
30                     }
31                 }
32             }
33         }
34         return false
35     }
36 }

432ms

 1 class Solution {
 2     func canCross(_ stones: [Int]) -> Bool {
 3         var m:[Int:Set<Int>] = [Int:Set<Int>]()
 4         var dp:[Int] = [Int](repeating:0,count:stones.count)
 5         var set:Set<Int> = [0]
 6         m[0] = set
 7         var k:Int = 0
 8         for i in 1..<stones.count
 9         {
10             while(dp[k] + 1 < stones[i] - stones[k])
11             {
12                 k += 1
13             }
14             for j in k..<i
15             {
16                 var t:Int = stones[i] - stones[j]
17                 if m[j] != nil && (m[j]!.contains(t - 1) || m[j]!.contains(t) || m[j]!.contains(t + 1))
18                 {
19                     if m[i] == nil
20                     {
21                         var set2:Set<Int> = [t]
22                         m[i] = set2
23                     }
24                     else
25                     {
26                         m[i]!.insert(t)
27                     }
28                     
29                     dp[i] = max(dp[i], t)
30                 }
31             }
32         }
33         return dp.last! > 0
34     }
35 }

528ms

 1 class Solution {
 2     func canCross(_ stones: [Int]) -> Bool {
 3         var result = [Int: Set<Int>]()
 4         for stone in stones {
 5             result[stone] = Set<Int>()
 6         }
 7         result[0]!.insert(0)
 8         for stone in stones {
 9             for step in result[stone]! {
10                 for newStep in step - 1...step + 1 {
11                     if newStep > 0 && result[stone + newStep] != nil {
12                         result[stone + newStep]!.insert(newStep)
13                     }
14                 }
15             }
16         }
17         return result[stones.last!]!.count > 0
18     }
19 }

536ms

 1 class Solution {
 2     func canCross(_ stones: [Int]) -> Bool {
 3         guard !stones.isEmpty else {
 4             return false
 5         }
 6 
 7         let stonesSet = Set(stones)
 8         var dict = [Int: Set<Int>]()
 9         dict[0] = [0]
10 
11         for stone in stones {
12             if let steps = dict[stone] {
13                 for step in steps {
14                     for possible in (step - 1)...(step + 1) {
15                         if possible > 0 {
16                             let nextLocation = stone + possible
17                             if stonesSet.contains(nextLocation) {
18                                 if var locationSteps = dict[nextLocation] {
19                                     locationSteps.insert(possible)
20                                     dict[nextLocation] = locationSteps
21                                 } else {
22                                     dict[nextLocation] = [possible]
23                                 }
24                             }
25                         }
26                     }
27                 }
28             }
29         }
30 
31         let last = stones.last!
32         return dict[last] != nil
33     }
34 }

564ms

 1 class Solution {
 2     func canCross(_ stones: [Int]) -> Bool {
 3         var map: [Int: Set<Int>] = [:]
 4         
 5         // stores possible steps to get to this stone
 6         for stone in stones {
 7             map[stone] = Set<Int>()
 8         }
 9         map[0]?.insert(0)
10         for stone in stones {
11             for k in map[stone]! {
12                 for step in k-1...k+1 {
13                     let newDest = stone + step
14                     if step > 0 && map.keys.contains(newDest) {
15                         map[newDest]!.insert(step)
16                     }
17                 }
18             }
19         }
20         return !map[stones.last!]!.isEmpty
21     }
22 }

704ms

 1 class Solution {
 2     func canCross(_ stones: [Int]) -> Bool {
 3         // Key is stone and array is possible steps to reach stone
 4         var map = [Int: Set<Int>]() 
 5         
 6         // fill all the stone as key to map
 7         for stone in stones {
 8             map[stone] = Set<Int>()
 9         }
10         
11         // Initialize the first first index of map with "0"
12         map[0] = [0]
13         
14         // Loop through each stone and fill the map with threee possible values        
15         for stone in stones {
16            if let stonesArray = map[stone] {
17             for possibleWay in stonesArray {
18                 
19                 for step in (possibleWay - 1 ... possibleWay + 1) {
20                     
21                     if let value = map[stone + step] {
22                         var steps = value
23                         steps.insert(step)
24                         map[stone + step] = steps
25                     }
26                 }
27              }
28         }
29         }
30         if let lastValues = map[stones[stones.count - 1]] {
31                 return lastValues.count > 0
32             }
33         return false
34     }
35 }

820ms

 1 class Solution {
 2     func canCross(_ stones: [Int]) -> Bool {
 3         var jump:[Int:Set<Int>] = [0:[1], 1:[1,2]]
 4         for i in 0..<stones.count{  
 5             if jump[stones[i]] == nil{
 6                 continue
 7             }
 8             let steps = jump[stones[i]]!
 9             for step in steps{
10                 if stones[i] + step == stones.last{
11                     return true
12                 }
13                 jump[stones[i]+step] = jump[stones[i]+step] ?? [step]
14                 if step-1 > 0{
15                     jump[stones[i]+step]?.insert(step-1)
16                 }
17                 jump[stones[i]+step]?.insert(step)
18                 jump[stones[i]+step]?.insert(step+1)
19                 
20  
                       
                    
                    

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
swift学习笔记之-析构过程发布时间:2022-07-13
下一篇:
[Swift]LeetCode86.分隔链表|PartitionList发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap