Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Merge Intervals solution and difficulty #287

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions DP/MaximumSubarray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
*/

class MaximumSubarray {
func maxSubArray(nums: [Int]) -> Int {
var max_current = nums[0]
var max_global = nums[0]

for i in 1..<nums.count {
max_current = max(max_current + nums[i], nums[i])
max_global = max(max_current, max_global)
func maxSubArray(_ nums: [Int]) -> Int {
var nums = nums
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to use a local nums

var ret = nums.removeFirst()
var curr = ret
for num in nums {
curr = max(num, curr + num)
ret = max(ret, curr)
}

return max_global
return ret
}
}
}
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@
[Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/)| [Swift](./Sort/TopKFrequentElements.swift)| Medium| O(nlogn)| O(n)|
[Meeting Rooms](https://leetcode.com/problems/meeting-rooms/)| [Swift](./Sort/MeetingRooms.swift)| Easy| O(nlogn)| O(1)|
[Meeting Rooms II](https://leetcode.com/problems/meeting-rooms-ii/)| [Swift](./Sort/MeetingRoomsII.swift)| Medium| O(nlogn)| O(n)|
[Merge Intervals](https://leetcode.com/problems/merge-intervals/)| [Swift](./Sort/MergeIntervals.swift)| Hard| O(nlogn)| O(n)|
[Merge Intervals](https://leetcode.com/problems/merge-intervals/)| [Swift](./Sort/MergeIntervals.swift)| Medium| O(nlogn)| O(n)|
[Alien Dictionary](https://leetcode.com/problems/alien-dictionary/)| [Swift](./Graph/AlienDictionary.swift)| Hard| O(nm)| O(nm)|
[Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/)| [Swift](./Sort/KthLargestElementInArray.swift)| Medium| O(nlogn)| O(n)|
[Array Partition I](https://leetcode.com/problems/array-partition-i/description/)| [Swift](./Sort/ArrayPartitionI.swift)|Easy| O(nlogn)| O(n)|
Expand Down Expand Up @@ -405,7 +405,7 @@
[Number of Islands](https://leetcode.com/problems/number-of-islands/)| [Swift](./DFS/NumberofIslands.swift)| Medium| ★★★★|
[Summary Ranges](https://leetcode.com/problems/summary-ranges/)| [Swift](./Array/SummaryRanges.swift)| Medium| ★★★★|
[Perfect Squares](https://leetcode.com/problems/perfect-squares/)| [Swift](./DP/PerfectSquares.swift)| Medium| ★★★★|
[Merge Intervals](https://leetcode.com/problems/merge-intervals/)| [Swift](./Sort/MergeIntervals.swift)| Hard| ★★★|
[Merge Intervals](https://leetcode.com/problems/merge-intervals/)| [Swift](./Sort/MergeIntervals.swift)| Medium| ★★★|
[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/)| [Swift](./Stack/ValidParentheses.swift)| Easy| ★★★|
[Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/)| [Swift](./Math/TrappingRainWater.swift)| Hard| ★★|
[Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/)| [Swift](./LinkedList/MergeKSortedLists.swift)| Hard| ★★|
Expand All @@ -432,7 +432,7 @@
[Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)| [Swift](./DFS/LetterCombinationsPhoneNumber.swift)| Medium| ★★★★|
[Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/)| [Swift](./LinkedList/MergeKSortedLists.swift)| Hard| ★★★★|
[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)| [Swift](./LinkedList/ReverseLinkedList.swift)| Easy| ★★★|
[Merge Intervals](https://leetcode.com/problems/merge-intervals/)| [Swift](./Sort/MergeIntervals.swift)| Hard| ★★★|
[Merge Intervals](https://leetcode.com/problems/merge-intervals/)| [Swift](./Sort/MergeIntervals.swift)| Medium| ★★★|
[Number of Islands](https://leetcode.com/problems/number-of-islands/)| [Swift](./DFS/NumberofIslands.swift)| Medium| ★★★|
[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)| [Swift](./LinkedList/ReverseLinkedList.swift)| Easy| ★★★|
[Expression Add Operators](https://leetcode.com/problems/expression-add-operators/)| [Swift](./DFS/ExpressionAddOperators.swift)| Hard| ★★★|
Expand Down Expand Up @@ -481,7 +481,7 @@
| ----- | -------- | ---------- | --------- |
[Maximum Subarray](https://leetcode.com/problems/maximum-subarray/)| [Swift](./DP/MaximumSubarray.swift)| Medium| ★★★★★★|
[Pow(x, n)](https://leetcode.com/problems/isomorphic-strings/)| [Swift](./Math/Pow.swift)| Medium| ★★★★★★|
[Merge Intervals](https://leetcode.com/problems/merge-intervals/)| [Swift](./Sort/MergeIntervals.swift)| Hard| ★★★★★★|
[Merge Intervals](https://leetcode.com/problems/merge-intervals/)| [Swift](./Sort/MergeIntervals.swift)| Medium| ★★★★★★|
[Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/)| [Swift](./String/IsomorphicStrings.swift)| Easy| ★★★★★★|
[Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/)| [Swift](./Search/SearchInRotatedSortedArray.swift)| Hard| ★★★★★|
[Search for a Range](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/)| [Swift](./Search/SearchForARange.swift)| Medium| ★★★★★|
Expand Down Expand Up @@ -833,7 +833,7 @@
| [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 |
| [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 |
| [Swift](./Sort/MergeIntervals.swift) | 56 | [Merge Intervals](https://oj.leetcode.com/problems/merge-intervals/) | 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 |
Expand Down
53 changes: 19 additions & 34 deletions Sort/MergeIntervals.swift
Original file line number Diff line number Diff line change
@@ -1,44 +1,29 @@
/**
* Question Link: https://leetcode.com/problems/merge-intervals/
* Primary idea: Sort the original intervals and then append them one by one
* Primary idea: Sort intervals by leading integer in ascending order and append based on their leading and trailing integers.
* Time Complexity: O(nlogn), Space Complexity: O(n)
*
* 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 MergeIntervals {
func merge(intervals: [Interval]) -> [Interval] {
var result = [Interval]()

let intervals = intervals.sorted {
if $0.start != $1.start {
return $0.start < $1.start
func merge(_ intervals: [[Int]]) -> [[Int]] {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your solution works but it is almost the same logic as the original one. Since it does not help improve the runtime complexity or simplify the code, I will not merge it.

guard intervals.count > 1 else { return intervals }
let intervals = intervals.sorted { $0[0] < $1[0] }
var ret: [[Int]] = []
var i = 1
var last = intervals[0]
while i < intervals.endIndex {
let curr = intervals[i]
if curr[0] <= last[1] {
if curr[1] > last[1] {
last[1] = curr[1]
}
} else {
return $0.end < $1.end
ret.append(last)
last = curr
}
guard i != intervals.endIndex - 1 else { ret.append(last); break }
i += 1
}

for interval in intervals {
guard let last = result.last else {
result.append(interval)
continue
}

if last.end < interval.start {
result.append(interval)
} else {
last.end = max(last.end, interval.end)
}
}

return result
return ret
}
}
}