Skip to content

Commit 3bbbf4e

Browse files
committed
[Array] Add a solution to Smallest Range
1 parent 2b1c75e commit 3bbbf4e

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

Diff for: Array/SmallestRange.swift

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/smallest-range/
3+
* Primary idea: Merge K lists + Minimum Window Substring
4+
* Time Complexity: O(nm), Space Complexity: O(nm)
5+
*
6+
*/
7+
8+
class SmallestRange {
9+
func smallestRange(_ nums: [[Int]]) -> [Int] {
10+
let mergedNums = merge(nums)
11+
12+
var left = 0, diff = Int.max, res = [Int]()
13+
var numsIndexFreq = Dictionary((0..<nums.count).map { ($0, 0) }, uniquingKeysWith: +), count = 0
14+
15+
for (right, numIndex) in mergedNums.enumerated() {
16+
if numsIndexFreq[numIndex.1]! == 0 {
17+
count += 1
18+
}
19+
20+
numsIndexFreq[numIndex.1]! += 1
21+
22+
while count == nums.count {
23+
if diff > mergedNums[right].0 - mergedNums[left].0 {
24+
diff = mergedNums[right].0 - mergedNums[left].0
25+
res = [mergedNums[left], mergedNums[right]].map { $0.0 }
26+
}
27+
28+
if numsIndexFreq[mergedNums[left].1]! == 1 {
29+
count -= 1
30+
}
31+
numsIndexFreq[mergedNums[left].1]! -= 1
32+
33+
left += 1
34+
}
35+
}
36+
37+
return res
38+
}
39+
40+
fileprivate func merge(_ nums: [[Int]]) -> [(Int, Int)] {
41+
var res = [(Int, Int)]()
42+
43+
for (numsIndex, nums) in nums.enumerated() {
44+
for num in nums {
45+
res.append((num, numsIndex))
46+
}
47+
}
48+
49+
return res.sorted { return $0.0 < $1.0 }
50+
}
51+
}

Diff for: README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* [Microsoft](#microsoft)
2828

2929
## Progress
30-
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 229 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
30+
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 230 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
3131

3232

3333
## Array
@@ -54,8 +54,9 @@
5454
[Summary Ranges](https://leetcode.com/problems/summary-ranges/)| [Swift](./Array/SummaryRanges.swift)| Medium| O(n)| O(n)|
5555
[Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance/)| [Swift](./Array/ShortestWordDistance.swift)| Easy| O(n)| O(1)|
5656
[Shortest Word Distance III](https://leetcode.com/problems/shortest-word-distance-iii/)| [Swift](./Array/ShortestWordDistanceIII.swift)| Medium| O(n)| O(1)|
57-
[Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/)| [Swift](./Array/MinimumSizeSubarraySum.swift)| Easy| O(n)| O(1)|
58-
[Maximum Size Subarray Sum Equals k](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/)| [Swift](./Array/MaximumSizeSubarraySumEqualsK.swift)| Easy| O(n)| O(n)|
57+
[Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/)| [Swift](./Array/MinimumSizeSubarraySum.swift)| Medium| O(n)| O(1)|
58+
[Maximum Size Subarray Sum Equals k](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/)| [Swift](./Array/MaximumSizeSubarraySumEqualsK.swift)| Medium| O(n)| O(n)|
59+
[Smallest Range](https://leetcode.com/problems/smallest-range/)| [Swift](./Array/SmallestRange.swift)| Hard | O(nm)| O(nm)|
5960
[Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/)| [Swift](./Array/ProductExceptSelf.swift)| Medium| O(n)| O(n)|
6061
[Rotate Array](https://leetcode.com/problems/rotate-array/)| [Swift](./Array/RotateArray.swift)| Easy| O(n)| O(1)|
6162
[Rotate Image](https://leetcode.com/problems/rotate-image/)| [Swift](./Array/RotateImage.swift)| Medium| O(n^2)| O(1)|

0 commit comments

Comments
 (0)