-
Notifications
You must be signed in to change notification settings - Fork 5
/
leetcode-403-frog-jump.swift
168 lines (119 loc) · 4.94 KB
/
leetcode-403-frog-jump.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
Copyright (c) 2020 David Seek, LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
https://leetcode.com/problems/frog-jump/
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.
*/
/**
Big O Annotation
Time complexity O(3^n) because of the 3 different choices we have,
where n is the amount of elements in stones.
Space complexity O(n) where n is the amount of elements in stones.
*/
typealias Jump = (stone: Int, distance: Int)
func canCross(_ stones: [Int]) -> Bool {
// Reference to the goal, to make the code cleaner
let goal: Int = stones[stones.count - 1]
// Set of stones for O(1) .contains operation
let stones: Set<Int> = Set(stones)
// All variations of distance altering
let variations: [Int] = [
-1,
0,
1
]
// Stones we have visited before to spead up the DFS
var visited: [String: Bool] = [:]
// Init a stack...
var stack = Stack()
// ... and add the first stone and distance
stack.push((stone: 0, distance: 0))
// While the stack is not empty...
while !stack.isEmpty {
// Pop the last one
let jump = stack.pop()!
// And mark it visited
visited["\(jump)"] = true
// For all 3 variations...
for variation in variations {
// Calculate the new distance
let distance = (jump.distance + variation)
// Skip if the distance is not forwards
guard distance > 0 else {
continue
}
// Get the new position
let newPosition = jump.stone + distance
// Check if it's our goal
if newPosition == goal {
return true
}
// Check if it's part of the array
if stones.contains(newPosition) {
// If so, get a new Jump
let tuple: Jump = (stone: newPosition, distance: distance)
// And if we haven't been there before,
if visited["\(tuple)"] == nil {
// ... add it.
stack.push(tuple)
}
}
}
}
/**
If we fall all the way through the while loop,
then we know we can't reach the end and have to...
*/
return false
}
/**
Simple Stack API.
All operations O(1) in time.
*/
struct Stack {
private var stack: [Jump] = []
public var isEmpty: Bool {
return stack.isEmpty
}
init() {}
public mutating func push(_ element: Jump) {
stack.append(element)
}
public mutating func pop() -> Jump? {
return stack.popLast()
}
}