Skip to content

Commit 283359f

Browse files
committed
添加 problem 632
1 parent fd6c824 commit 283359f

File tree

4 files changed

+149
-1
lines changed

4 files changed

+149
-1
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package leetcode
2+
3+
import (
4+
"math"
5+
"sort"
6+
)
7+
8+
func smallestRange(nums [][]int) []int {
9+
numList, left, right, count, freqMap, res, length := []element{}, 0, -1, 0, map[int]int{}, make([]int, 2), math.MaxInt64
10+
for i, ns := range nums {
11+
for _, v := range ns {
12+
numList = append(numList, element{val: v, index: i})
13+
}
14+
}
15+
sort.Sort(SortByVal{numList})
16+
for left < len(numList) {
17+
if right+1 < len(numList) && count < len(nums) {
18+
right++
19+
if freqMap[numList[right].index] == 0 {
20+
count++
21+
}
22+
freqMap[numList[right].index]++
23+
} else {
24+
if count == len(nums) {
25+
if numList[right].val-numList[left].val < length {
26+
length = numList[right].val - numList[left].val
27+
res[0] = numList[left].val
28+
res[1] = numList[right].val
29+
}
30+
}
31+
freqMap[numList[left].index]--
32+
if freqMap[numList[left].index] == 0 {
33+
count--
34+
}
35+
left++
36+
}
37+
}
38+
return res
39+
}
40+
41+
type element struct {
42+
val int
43+
index int
44+
}
45+
46+
type elements []element
47+
48+
// Len define
49+
func (p elements) Len() int { return len(p) }
50+
51+
// Swap define
52+
func (p elements) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
53+
54+
// SortByVal define
55+
type SortByVal struct{ elements }
56+
57+
// Less define
58+
func (p SortByVal) Less(i, j int) bool {
59+
return p.elements[i].val < p.elements[j].val
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question632 struct {
9+
para632
10+
ans632
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para632 struct {
16+
one [][]int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans632 struct {
22+
one []int
23+
}
24+
25+
func Test_Problem632(t *testing.T) {
26+
27+
qs := []question632{
28+
29+
question632{
30+
para632{[][]int{[]int{4, 10, 15, 24, 26}, []int{0, 9, 12, 20}, []int{5, 18, 22, 30}}},
31+
ans632{[]int{20, 24}},
32+
},
33+
}
34+
35+
fmt.Printf("------------------------Leetcode Problem 632------------------------\n")
36+
37+
for _, q := range qs {
38+
_, p := q.ans632, q.para632
39+
fmt.Printf("【input】:%v 【output】:%v\n", p, smallestRange(p.one))
40+
}
41+
fmt.Printf("\n\n\n")
42+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# [632. Smallest Range Covering Elements from K Lists](https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists/)
2+
3+
4+
## 题目:
5+
6+
You have `k` lists of sorted integers in ascending order. Find the **smallest** range that includes at least one number from each of the `k` lists.
7+
8+
We define the range [a,b] is smaller than range [c,d] if `b-a < d-c` or `a < c` if `b-a == d-c`.
9+
10+
**Example 1:**
11+
12+
Input: [[4,10,15,24,26], [0,9,12,20], [5,18,22,30]]
13+
Output: [20,24]
14+
Explanation:
15+
List 1: [4, 10, 15, 24,26], 24 is in range [20,24].
16+
List 2: [0, 9, 12, 20], 20 is in range [20,24].
17+
List 3: [5, 18, 22, 30], 22 is in range [20,24].
18+
19+
**Note:**
20+
21+
1. The given list may contain duplicates, so ascending order means >= here.
22+
2. 1 <= `k` <= 3500
23+
3. -10^5 <= `value of elements` <= 10^5.
24+
25+
26+
## 题目大意
27+
28+
你有 k 个升序排列的整数数组。找到一个最小区间,使得 k 个列表中的每个列表至少有一个数包含在其中。
29+
30+
我们定义如果 b-a < d-c 或者在 b-a == d-c 时 a < c,则区间 [a,b][c,d] 小。
31+
32+
注意:
33+
34+
- 给定的列表可能包含重复元素,所以在这里升序表示 >= 。
35+
- 1 <= k <= 3500
36+
- -105 <= 元素的值 <= 105
37+
- 对于使用Java的用户,请注意传入类型已修改为List<List<Integer>>。重置代码模板后可以看到这项改动。
38+
39+
40+
41+
## 解题思路
42+
43+
44+
- 给出 K 个数组,要求在这 K 个数组中找到一个区间,至少能包含这 K 个数组中每个数组中的一个元素。
45+
- 这一题是第 76 题的变种版。第 76 题是用滑动窗口来解答的,它要求在母字符串 S 中找到最小的子串能包含 T 串的所有字母。这一题类似的,可以把母字符串看成 K 个数组合并起来的大数组,那么 T 串是由 K 个数组中每个数组中抽一个元素出来组成的。求的区间相同,都是能包含 T 的最小区间。另外一个区别在于,第 76 题里面都是字符串,这一题都是数字,在最终拼接成 T 串的时候需要保证 K 个数组中每个都有一个元素,所以理所当然的想到需要维护每个元素所在数组编号。经过上述的转换,可以把这道题转换成第 76 题的解法了。
46+
- 在具体解题过程中,用 map 来维护窗口内 K 个数组出现的频次。时间复杂度 O(n*log n),空间复杂度是O(n)。

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@
696696
| 0629 | K Inverse Pairs Array | | 29.30% | Hard | |
697697
| 0630 | Course Schedule III | | 31.90% | Hard | |
698698
| 0631 | Design Excel Sum Formula | | 29.30% | Hard | |
699-
| 0632 | Smallest Range | | 47.90% | Hard | |
699+
| 0632 | Smallest Range | [Go](https://github.com/halfrost/LeetCode-Go/tree/master/Algorithms/0632.%20Smallest%20Range%20Covering%20Elements%20from%20K%20Lists) | 47.90% | Hard | |
700700
| 0633 | Sum of Square Numbers | | 32.70% | Easy | |
701701
| 0634 | Find the Derangement of An Array | | 37.80% | Medium | |
702702
| 0635 | Design Log Storage System | | 54.40% | Medium | |

0 commit comments

Comments
 (0)