diff --git a/solution/0300-0399/0368.Largest Divisible Subset/README.md b/solution/0300-0399/0368.Largest Divisible Subset/README.md index a76a8af57855d..fc5f3d1779c63 100644 --- a/solution/0300-0399/0368.Largest Divisible Subset/README.md +++ b/solution/0300-0399/0368.Largest Divisible Subset/README.md @@ -53,7 +53,29 @@ ```python - +class Solution: + def largestDivisibleSubset(self, nums: List[int]) -> List[int]: + nums.sort() + n = len(nums) + f, p = [0] * n, [0] * n + for i in range(n): + l, pre = 1, i + for j in range(n): + if nums[i] % nums[j] == 0 and f[j] + 1 > l: + l = f[j] + 1 + pre = j + f[i] = l + p[i] = pre + max_len, max_index = 0, 0 + for i, v in enumerate(f): + if max_len < v: + max_len = v + max_index = i + ans = [] + while len(ans) < max_len: + ans.append(nums[max_index]) + max_index = p[max_index] + return ans[::-1] ``` ### **Java** @@ -61,7 +83,38 @@ ```java - +class Solution { + public List largestDivisibleSubset(int[] nums) { + Arrays.sort(nums); + int n = nums.length; + int[] f = new int[n], p = new int[n]; + for (int i = 0; i < n; i++) { + int l = 1, pre = i; + for (int j = 0; j < i; j++) { + if (nums[i] % nums[j] == 0 && f[j] + 1 > l) { + l = f[j] + 1; + pre = j; + } + } + f[i] = l; + p[i] = pre; + } + int maxLen = 0, maxIndex = 0; + for (int i = 0; i < n; i++) { + if (f[i] > maxLen) { + maxLen = f[i]; + maxIndex = i; + } + } + List ans = new ArrayList<>(); + while (ans.size() < maxLen) { + ans.add(nums[maxIndex]); + maxIndex = p[maxIndex]; + } + Collections.reverse(ans); + return ans; + } +} ``` ### **...** diff --git a/solution/0300-0399/0368.Largest Divisible Subset/README_EN.md b/solution/0300-0399/0368.Largest Divisible Subset/README_EN.md index 1d26fe5e9194f..7ec32e2671367 100644 --- a/solution/0300-0399/0368.Largest Divisible Subset/README_EN.md +++ b/solution/0300-0399/0368.Largest Divisible Subset/README_EN.md @@ -45,13 +45,66 @@ ### **Python3** ```python - +class Solution: + def largestDivisibleSubset(self, nums: List[int]) -> List[int]: + nums.sort() + n = len(nums) + f, p = [0] * n, [0] * n + for i in range(n): + l, pre = 1, i + for j in range(n): + if nums[i] % nums[j] == 0 and f[j] + 1 > l: + l = f[j] + 1 + pre = j + f[i] = l + p[i] = pre + max_len, max_index = 0, 0 + for i, v in enumerate(f): + if max_len < v: + max_len = v + max_index = i + ans = [] + while len(ans) < max_len: + ans.append(nums[max_index]) + max_index = p[max_index] + return ans[::-1] ``` ### **Java** ```java - +class Solution { + public List largestDivisibleSubset(int[] nums) { + Arrays.sort(nums); + int n = nums.length; + int[] f = new int[n], p = new int[n]; + for (int i = 0; i < n; i++) { + int l = 1, pre = i; + for (int j = 0; j < i; j++) { + if (nums[i] % nums[j] == 0 && f[j] + 1 > l) { + l = f[j] + 1; + pre = j; + } + } + f[i] = l; + p[i] = pre; + } + int maxLen = 0, maxIndex = 0; + for (int i = 0; i < n; i++) { + if (f[i] > maxLen) { + maxLen = f[i]; + maxIndex = i; + } + } + List ans = new ArrayList<>(); + while (ans.size() < maxLen) { + ans.add(nums[maxIndex]); + maxIndex = p[maxIndex]; + } + Collections.reverse(ans); + return ans; + } +} ``` ### **...** diff --git a/solution/0300-0399/0368.Largest Divisible Subset/Solution.java b/solution/0300-0399/0368.Largest Divisible Subset/Solution.java new file mode 100644 index 0000000000000..f3c3b23068127 --- /dev/null +++ b/solution/0300-0399/0368.Largest Divisible Subset/Solution.java @@ -0,0 +1,32 @@ +class Solution { + public List largestDivisibleSubset(int[] nums) { + Arrays.sort(nums); + int n = nums.length; + int[] f = new int[n], p = new int[n]; + for (int i = 0; i < n; i++) { + int l = 1, pre = i; + for (int j = 0; j < i; j++) { + if (nums[i] % nums[j] == 0 && f[j] + 1 > l) { + l = f[j] + 1; + pre = j; + } + } + f[i] = l; + p[i] = pre; + } + int maxLen = 0, maxIndex = 0; + for (int i = 0; i < n; i++) { + if (f[i] > maxLen) { + maxLen = f[i]; + maxIndex = i; + } + } + List ans = new ArrayList<>(); + while (ans.size() < maxLen) { + ans.add(nums[maxIndex]); + maxIndex = p[maxIndex]; + } + Collections.reverse(ans); + return ans; + } +} \ No newline at end of file diff --git a/solution/0300-0399/0368.Largest Divisible Subset/Solution.py b/solution/0300-0399/0368.Largest Divisible Subset/Solution.py new file mode 100644 index 0000000000000..b7971dbca5cc3 --- /dev/null +++ b/solution/0300-0399/0368.Largest Divisible Subset/Solution.py @@ -0,0 +1,23 @@ +class Solution: + def largestDivisibleSubset(self, nums: List[int]) -> List[int]: + nums.sort() + n = len(nums) + f, p = [0] * n, [0] * n + for i in range(n): + l, pre = 1, i + for j in range(n): + if nums[i] % nums[j] == 0 and f[j] + 1 > l: + l = f[j] + 1 + pre = j + f[i] = l + p[i] = pre + max_len, max_index = 0, 0 + for i, v in enumerate(f): + if max_len < v: + max_len = v + max_index = i + ans = [] + while len(ans) < max_len: + ans.append(nums[max_index]) + max_index = p[max_index] + return ans[::-1] \ No newline at end of file