FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode/src/main/java/L0368_LargestDivisibleSubset.java at master · LjyYano/LeetCode · GitHub
LjyYano
/
LeetCode
Public
Notifications
You must be signed in to change notification settings
Fork
125
Star
342
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode
/
src
/
main
/
java
/
L0368_LargestDivisibleSubset.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
93 lines (81 loc) · 2.94 KB
Breadcrumbs
LeetCode
/
src
/
main
/
java
/
L0368_LargestDivisibleSubset.java
Copy path
File metadata and controls
93 lines (81 loc) · 2.94 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Arrays
;
import
java
.
util
.
List
;
/**
* https://leetcode.cn/problems/largest-divisible-subset/
*
* 给你一个由 无重复 正整数组成的集合 nums ,请你找出并返回其中最大的整除子集 answer ,子集中每一对 (answer[i], answer[j]) 都应当满足:
* - answer[i] % answer[j] == 0,或
* - answer[j] % answer[i] == 0
* 如果存在多个有效解子集,返回其中任何一个均可。
*
* 示例 1:
* 输入:nums = [1,2,3]
* 输出:[1,2]
* 解释:[1,3] 也会被视为正确答案。
*
* 示例 2:
* 输入:nums = [1,2,4,8]
* 输出:[1,2,4,8]
*
* 提示:
* - 1 <= nums.length <= 1000
* - 1 <= nums[i] <= 2 * 10⁹
* - nums 中的所有整数 互不相同
*/
public
class
L0368_LargestDivisibleSubset
{
public
List
<
Integer
>
largestDivisibleSubset
(
int
[]
nums
) {
int
n
=
nums
.
length
;
// 结果列表
List
<
Integer
>
result
=
new
ArrayList
<>();
if
(
n
==
0
) {
return
result
;
}
// 先将数组排序
Arrays
.
sort
(
nums
);
// dp[i] 表示以 nums[i] 结尾的最大整除子集的长度
int
[]
dp
=
new
int
[
n
];
// prev[i] 记录前一个数的索引,用于重建最大整除子集
int
[]
prev
=
new
int
[
n
];
// 初始化 dp 和 prev 数组
Arrays
.
fill
(
dp
,
1
);
Arrays
.
fill
(
prev
, -
1
);
// 记录最大长度和对应的索引
int
maxLen
=
1
;
int
maxIndex
=
0
;
// 动态规划过程
for
(
int
i
=
1
;
i
<
n
;
i
++) {
for
(
int
j
=
0
;
j
<
i
;
j
++) {
// 如果 nums[i] 能整除 nums[j],则可以将 nums[i] 加入以 nums[j] 结尾的子集
if
(
nums
[
i
] %
nums
[
j
] ==
0
&&
dp
[
j
] +
1
>
dp
[
i
]) {
dp
[
i
] =
dp
[
j
] +
1
;
prev
[
i
] =
j
;
}
}
// 更新最大长度和对应的索引
if
(
dp
[
i
] >
maxLen
) {
maxLen
=
dp
[
i
];
maxIndex
=
i
;
}
}
// 重建最大整除子集
while
(
maxIndex
!= -
1
) {
result
.
add
(
0
,
nums
[
maxIndex
]);
maxIndex
=
prev
[
maxIndex
];
}
return
result
;
}
public
static
void
main
(
String
[]
args
) {
L0368_LargestDivisibleSubset
solution
=
new
L0368_LargestDivisibleSubset
();
// 测试用例 1
int
[]
nums1
= {
1
,
2
,
3
};
System
.
out
.
println
(
"测试用例 1:"
);
System
.
out
.
println
(
"输入:"
+
Arrays
.
toString
(
nums1
));
System
.
out
.
println
(
"输出:"
+
solution
.
largestDivisibleSubset
(
nums1
));
// 测试用例 2
int
[]
nums2
= {
1
,
2
,
4
,
8
};
System
.
out
.
println
(
"
\n
测试用例 2:"
);
System
.
out
.
println
(
"输入:"
+
Arrays
.
toString
(
nums2
));
System
.
out
.
println
(
"输出:"
+
solution
.
largestDivisibleSubset
(
nums2
));
}
}
Back
|
FazBrowse Home
|
New Git URL