Skip to content

Commit

Permalink
Merge pull request soapyigu#220 from zj9205/master
Browse files Browse the repository at this point in the history
Add solution for soapyigu#55 Jump Game and soapyigu#57. Insert Interval
  • Loading branch information
soapyigu authored May 17, 2018
2 parents 66467ca + 354c624 commit b6372aa
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
22 changes: 22 additions & 0 deletions DP/JumpGame.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Question Link: https://leetcode.com/problems/jump-game/
* Primary idea: check each position with the previous farest step can reach. If i > last farest step, means cannot reach
* Time Complexity: O(n), Space Complexity: O(1)
*
*/

class JumpGame {
func canJump(_ nums: [Int]) -> Bool {
var max = 0

for i in 0 ..< nums.count {
let farestStep = i + nums[i]
if i > max {
return false
}
max = max > farestStep ? max : farestStep
}

return true
}
}
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
[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)|
[Burst Ballons](https://leetcode.com/problems/burst-balloons/)| [Swift](./DP/BurstBalloons.swift)| Hard| O(n^3)| O(n)|
[Frog Jump](https://leetcode.com/problems/frog-jump/)| [Swift](./DP/FrogJump.swift)| Hard| O(n^2)| O(n)|
[Jump Game](https://leetcode.com/problems/jump-game/)| [Swift](./DP/JumpGame.swift)| Medium| O(n)| O(1)|

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

## Union Find
| Title | Solution | Difficulty | Time | Space |
Expand Down Expand Up @@ -752,9 +754,9 @@
| | 60 | [Permutation Sequence](https://oj.leetcode.com/problems/permutation-sequence/) | Medium |
| [Swift](./Array/SpiralMatrixII.swift) | 59 | [Spiral Matrix II](https://oj.leetcode.com/problems/spiral-matrix-ii/) | Medium |
| [Swift](./String/LengthLastWord.swift) | 58 | [Length of Last Word](https://oj.leetcode.com/problems/length-of-last-word/) | Easy |
| | 57 | [Insert Interval](https://oj.leetcode.com/problems/insert-interval/) | Hard |
| [Swift](./Sort/InsertInterval.swift) | 57 | [Insert Interval](https://oj.leetcode.com/problems/insert-interval/) | Hard |
| [Swift](./Sort/MergeIntervals.swift) | 56 | [Merge Intervals](https://oj.leetcode.com/problems/merge-intervals/) | Hard |
| | 55 | [Jump Game](https://oj.leetcode.com/problems/jump-game/) | Medium |
| [Swift](./DP/JumpGame.swift) | 55 | [Jump Game](https://oj.leetcode.com/problems/jump-game/) | Medium |
| [Swift](./Array/SpiralMatrix.swift) | 54 | [Spiral Matrix](https://oj.leetcode.com/problems/spiral-matrix/) | Medium |
| [Swift](./DP/MaximumSubarray.swift) | 53 | [Maximum Subarray](https://oj.leetcode.com/problems/maximum-subarray/) | Medium |
| [Swift](./DFS/NQueensII.swift) | 52 | [N-Queens II](https://oj.leetcode.com/problems/n-queens-ii/) | Hard |
Expand Down
47 changes: 47 additions & 0 deletions Sort/InsertInterval.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Question Link: https://leetcode.com/problems/insert-interval/
* Primary idea: First, check if nuewInterval's start is larger than one interval's end.
* If so, save the index, otherwise save intervals
* Second, keep updating a new interval if nuewInterval's end is larger then one interval's start
* If cannot find more, append the new interval to the result array
* Final Step, append the rest intervals to the result array
*
* Time Complexity: O(n), Space Complexity: O(1),
*
* Definition for an interval.
* public class Interval {
* public var start: Int
* public var end: Int
* public init(_ start: Int, _ end: Int) {
* self.start = start
* self.end = end
* }
* }
*/

class InsertInterval {
func insert(_ intervals: [Interval], _ newInterval: Interval) -> [Interval] {
var index = 0
var result: [Interval] = []
var tempInterval = Interval(newInterval.start, newInterval.end)

while index < intervals.count && newInterval.start > intervals[index].end {
result.append(intervals[index])
index += 1
}

while index < intervals.count && newInterval.end >= intervals[index].start {
let minStart = min(tempInterval.start, intervals[index].start)
let maxEnd = max(tempInterval.end, intervals[index].end)
tempInterval = Interval(minStart, maxEnd)
index += 1
}
result.append(tempInterval)

for i in index ..< intervals.count {
result.append(intervals[i])
}

return result
}
}

0 comments on commit b6372aa

Please sign in to comment.