Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
algorithmzuo
algorithm-journey
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
algorithm-journey
/
src
/
class091
/
Code02_SmallestRange.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
56 lines (50 loc) · 1.7 KB
main
Breadcrumbs
algorithm-journey
/
src
/
class091
/
Code02_SmallestRange.java
Copy path
Top
File metadata and controls
Code
Blame
56 lines (50 loc) · 1.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package
class091
;
import
java
.
util
.
List
;
import
java
.
util
.
TreeSet
;
// 最小区间
// 你有k个非递减排列的整数列表
// 找到一个最小区间,使得k个列表中的每个列表至少有一个数包含在其中
// 测试链接 : https://leetcode.cn/problems/smallest-range-covering-elements-from-k-lists/
public
class
Code02_SmallestRange
{
public
static
class
Node
{
public
int
v
;
// 值
public
int
i
;
// 当前值来自哪个数组
public
int
j
;
// 当前值来自i号数组的什么位置
public
Node
(
int
a
,
int
b
,
int
c
) {
v
=
a
;
i
=
b
;
j
=
c
;
}
}
// 时间复杂度O(n*logk)
// n是数字总数,k是数组数量
public
static
int
[]
smallestRange
(
List
<
List
<
Integer
>>
nums
) {
int
k
=
nums
.
size
();
// 根据值排序
// 为什么排序的时候i要参与
// 因为有序表中比较相等的样本只会保留一个
// 为了值一样的元素都保留,于是i要参与排序
// 在有序表中的所有元素i必然都不同
TreeSet
<
Node
>
set
=
new
TreeSet
<>((
a
,
b
) ->
a
.
v
!=
b
.
v
? (
a
.
v
-
b
.
v
) : (
a
.
i
-
b
.
i
));
for
(
int
i
=
0
;
i
<
k
;
i
++) {
set
.
add
(
new
Node
(
nums
.
get
(
i
).
get
(
0
),
i
,
0
));
}
int
r
=
Integer
.
MAX_VALUE
;
// 记录最窄区间的宽度
int
a
=
0
;
// 记录最窄区间的开头
int
b
=
0
;
// 记录最窄区间的结尾
Node
max
,
min
;
while
(
set
.
size
() ==
k
) {
max
=
set
.
last
();
// 在有序表中,值最大的记录
min
=
set
.
pollFirst
();
// 在有序表中,值最小的记录,并弹出
if
(
max
.
v
-
min
.
v
<
r
) {
r
=
max
.
v
-
min
.
v
;
a
=
min
.
v
;
b
=
max
.
v
;
}
if
(
min
.
j
+
1
<
nums
.
get
(
min
.
i
).
size
()) {
set
.
add
(
new
Node
(
nums
.
get
(
min
.
i
).
get
(
min
.
j
+
1
),
min
.
i
,
min
.
j
+
1
));
}
}
return
new
int
[] {
a
,
b
};
}
}
You can’t perform that action at this time.