Skip to content

Commit b6372aa

Browse files
authored
Merge pull request soapyigu#220 from zj9205/master
Add solution for soapyigu#55 Jump Game and soapyigu#57. Insert Interval
2 parents 66467ca + 354c624 commit b6372aa

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

DP/JumpGame.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/jump-game/
3+
* Primary idea: check each position with the previous farest step can reach. If i > last farest step, means cannot reach
4+
* Time Complexity: O(n), Space Complexity: O(1)
5+
*
6+
*/
7+
8+
class JumpGame {
9+
func canJump(_ nums: [Int]) -> Bool {
10+
var max = 0
11+
12+
for i in 0 ..< nums.count {
13+
let farestStep = i + nums[i]
14+
if i > max {
15+
return false
16+
}
17+
max = max > farestStep ? max : farestStep
18+
}
19+
20+
return true
21+
}
22+
}

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@
211211
[Guess Number Higher or Lower II](https://leetcode.com/problems/guess-number-higher-or-lower-ii/)| [Swift](./DP/GuessNumberHigherOrLowerII.swift)| Medium| O(nlogn)| O(n^2)|
212212
[Burst Ballons](https://leetcode.com/problems/burst-balloons/)| [Swift](./DP/BurstBalloons.swift)| Hard| O(n^3)| O(n)|
213213
[Frog Jump](https://leetcode.com/problems/frog-jump/)| [Swift](./DP/FrogJump.swift)| Hard| O(n^2)| O(n)|
214+
[Jump Game](https://leetcode.com/problems/jump-game/)| [Swift](./DP/JumpGame.swift)| Medium| O(n)| O(1)|
214215

215216
## Depth-first search
216217
| Title | Solution | Difficulty | Time | Space |
@@ -312,6 +313,7 @@
312313
[Merge Intervals](https://leetcode.com/problems/merge-intervals/)| [Swift](./Sort/MergeIntervals.swift)| Hard| O(nlogn)| O(n)|
313314
[Alien Dictionary](https://leetcode.com/problems/alien-dictionary/)| [Swift](./Sort/AlienDictionary.swift)| Hard| O(nm)| O(nm)|
314315
[Array Partition I](https://leetcode.com/problems/array-partition-i/description/)| [Swift](./Sort/ArrayPartitionI.swift)|Easy| O(nlogn)| O(n)|
316+
[Insert Interval](https://leetcode.com/problems/insert-interval/description/)| [Swift](./Sort/InsertInterval.swift)|Hard| O(n)| O(1)|
315317

316318
## Union Find
317319
| Title | Solution | Difficulty | Time | Space |
@@ -752,9 +754,9 @@
752754
| | 60 | [Permutation Sequence](https://oj.leetcode.com/problems/permutation-sequence/) | Medium |
753755
| [Swift](./Array/SpiralMatrixII.swift) | 59 | [Spiral Matrix II](https://oj.leetcode.com/problems/spiral-matrix-ii/) | Medium |
754756
| [Swift](./String/LengthLastWord.swift) | 58 | [Length of Last Word](https://oj.leetcode.com/problems/length-of-last-word/) | Easy |
755-
| | 57 | [Insert Interval](https://oj.leetcode.com/problems/insert-interval/) | Hard |
757+
| [Swift](./Sort/InsertInterval.swift) | 57 | [Insert Interval](https://oj.leetcode.com/problems/insert-interval/) | Hard |
756758
| [Swift](./Sort/MergeIntervals.swift) | 56 | [Merge Intervals](https://oj.leetcode.com/problems/merge-intervals/) | Hard |
757-
| | 55 | [Jump Game](https://oj.leetcode.com/problems/jump-game/) | Medium |
759+
| [Swift](./DP/JumpGame.swift) | 55 | [Jump Game](https://oj.leetcode.com/problems/jump-game/) | Medium |
758760
| [Swift](./Array/SpiralMatrix.swift) | 54 | [Spiral Matrix](https://oj.leetcode.com/problems/spiral-matrix/) | Medium |
759761
| [Swift](./DP/MaximumSubarray.swift) | 53 | [Maximum Subarray](https://oj.leetcode.com/problems/maximum-subarray/) | Medium |
760762
| [Swift](./DFS/NQueensII.swift) | 52 | [N-Queens II](https://oj.leetcode.com/problems/n-queens-ii/) | Hard |

Sort/InsertInterval.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/insert-interval/
3+
* Primary idea: First, check if nuewInterval's start is larger than one interval's end.
4+
* If so, save the index, otherwise save intervals
5+
* Second, keep updating a new interval if nuewInterval's end is larger then one interval's start
6+
* If cannot find more, append the new interval to the result array
7+
* Final Step, append the rest intervals to the result array
8+
*
9+
* Time Complexity: O(n), Space Complexity: O(1),
10+
*
11+
* Definition for an interval.
12+
* public class Interval {
13+
* public var start: Int
14+
* public var end: Int
15+
* public init(_ start: Int, _ end: Int) {
16+
* self.start = start
17+
* self.end = end
18+
* }
19+
* }
20+
*/
21+
22+
class InsertInterval {
23+
func insert(_ intervals: [Interval], _ newInterval: Interval) -> [Interval] {
24+
var index = 0
25+
var result: [Interval] = []
26+
var tempInterval = Interval(newInterval.start, newInterval.end)
27+
28+
while index < intervals.count && newInterval.start > intervals[index].end {
29+
result.append(intervals[index])
30+
index += 1
31+
}
32+
33+
while index < intervals.count && newInterval.end >= intervals[index].start {
34+
let minStart = min(tempInterval.start, intervals[index].start)
35+
let maxEnd = max(tempInterval.end, intervals[index].end)
36+
tempInterval = Interval(minStart, maxEnd)
37+
index += 1
38+
}
39+
result.append(tempInterval)
40+
41+
for i in index ..< intervals.count {
42+
result.append(intervals[i])
43+
}
44+
45+
return result
46+
}
47+
}

0 commit comments

Comments
 (0)