-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
78 lines (69 loc) · 1.73 KB
/
main.go
File metadata and controls
78 lines (69 loc) · 1.73 KB
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
// Source: https://leetcode.com/problems/largest-divisible-subset
// Title: Largest Divisible Subset
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given a set of **distinct** positive integers `nums`, return the largest subset `answer` such that every pair `(answer[i], answer[j])` of elements in this subset satisfies:
//
// - `answer[i] % answer[j] == 0`, or
// - `answer[j] % answer[i] == 0`
//
// If there are multiple solutions, return any of them.
//
// **Example 1:**
//
// ```
// Input: nums = [1,2,3]
// Output: [1,2]
// Explanation: [1,3] is also accepted.
// ```
//
// **Example 2:**
//
// ```
// Input: nums = [1,2,4,8]
// Output: [1,2,4,8]
// ```
//
// **Constraints:**
//
// - `1 <= nums.length <= 1000`
// - `1 <= nums[i] <= 2 * 10^9`
// - All the integers in `nums` are **unique**.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
import (
"slices"
)
// Use DP
func largestDivisibleSubset(nums []int) []int {
n := len(nums)
slices.Sort(nums)
sizes := make([]int, n) // prefix subset size
divisors := make([]int, n)
for i := range n {
divisors[i] = -1
}
maxSize := 0
maxIdx := 0
for i, num := range nums {
sizes[i] = 0
for j, divisor := range nums[:i] {
if num%divisor == 0 && sizes[j] > sizes[i] {
sizes[i] = sizes[j]
divisors[i] = j
}
}
sizes[i]++ // include current number
if sizes[i] > maxSize {
maxSize = sizes[i]
maxIdx = i
}
}
res := make([]int, 0, maxSize)
for i := maxIdx; i != -1; i = divisors[i] {
res = append(res, nums[i])
}
return res
}