返回介绍

solution / 0300-0399 / 0368.Largest Divisible Subset / README_EN

发布于 2024-06-17 01:04:01 字数 3998 浏览 0 评论 0 收藏 0

368. Largest Divisible Subset

中文文档

Description

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 * 109
  • All the integers in nums are unique.

Solutions

Solution 1

class Solution:
  def largestDivisibleSubset(self, nums: List[int]) -> List[int]:
    nums.sort()
    n = len(nums)
    f = [1] * n
    k = 0
    for i in range(n):
      for j in range(i):
        if nums[i] % nums[j] == 0:
          f[i] = max(f[i], f[j] + 1)
      if f[k] < f[i]:
        k = i
    m = f[k]
    i = k
    ans = []
    while m:
      if nums[k] % nums[i] == 0 and f[i] == m:
        ans.append(nums[i])
        k, m = i, m - 1
      i -= 1
    return ans
class Solution {
  public List<Integer> largestDivisibleSubset(int[] nums) {
    Arrays.sort(nums);
    int n = nums.length;
    int[] f = new int[n];
    Arrays.fill(f, 1);
    int k = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < i; ++j) {
        if (nums[i] % nums[j] == 0) {
          f[i] = Math.max(f[i], f[j] + 1);
        }
      }
      if (f[k] < f[i]) {
        k = i;
      }
    }
    int m = f[k];
    List<Integer> ans = new ArrayList<>();
    for (int i = k; m > 0; --i) {
      if (nums[k] % nums[i] == 0 && f[i] == m) {
        ans.add(nums[i]);
        k = i;
        --m;
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> largestDivisibleSubset(vector<int>& nums) {
    sort(nums.begin(), nums.end());
    int n = nums.size();
    int f[n];
    int k = 0;
    for (int i = 0; i < n; ++i) {
      f[i] = 1;
      for (int j = 0; j < i; ++j) {
        if (nums[i] % nums[j] == 0) {
          f[i] = max(f[i], f[j] + 1);
        }
      }
      if (f[k] < f[i]) {
        k = i;
      }
    }
    int m = f[k];
    vector<int> ans;
    for (int i = k; m > 0; --i) {
      if (nums[k] % nums[i] == 0 && f[i] == m) {
        ans.push_back(nums[i]);
        k = i;
        --m;
      }
    }
    return ans;
  }
};
func largestDivisibleSubset(nums []int) (ans []int) {
  sort.Ints(nums)
  n := len(nums)
  f := make([]int, n)
  k := 0
  for i := 0; i < n; i++ {
    f[i] = 1
    for j := 0; j < i; j++ {
      if nums[i]%nums[j] == 0 {
        f[i] = max(f[i], f[j]+1)
      }
    }
    if f[k] < f[i] {
      k = i
    }
  }
  m := f[k]
  for i := k; m > 0; i-- {
    if nums[k]%nums[i] == 0 && f[i] == m {
      ans = append(ans, nums[i])
      k = i
      m--
    }
  }
  return
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文