diff --git a/DP/MaximumSubarray.swift b/DP/MaximumSubarray.swift index 571f24bf..ffd3f2a3 100644 --- a/DP/MaximumSubarray.swift +++ b/DP/MaximumSubarray.swift @@ -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.. Int { + var nums = 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 } -} \ No newline at end of file +} diff --git a/README.md b/README.md index 48d18af1..26eebcac 100644 --- a/README.md +++ b/README.md @@ -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)| @@ -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| ★★| @@ -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| ★★★| @@ -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| ★★★★★| @@ -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 | diff --git a/Sort/MergeIntervals.swift b/Sort/MergeIntervals.swift index b83f339e..fb55e058 100644 --- a/Sort/MergeIntervals.swift +++ b/Sort/MergeIntervals.swift @@ -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]] { + 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 } -} \ No newline at end of file +}