forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request soapyigu#220 from zj9205/master
Add solution for soapyigu#55 Jump Game and soapyigu#57. Insert Interval
- Loading branch information
Showing
3 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |