[LeetCode] Largest Divisible Subset 最大可整除的子集合


 

Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or Sj % Si = 0.

If there are multiple solutions, return any subset is fine.

Example 1:

nums: [1,2,3]

Result: [1,2] (of course, [1,3] will also be ok)

Example 2:

nums: [1,2,4,8]

Result: [1,2,4,8]

Credits:
Special thanks to @Stomach_ache for adding this problem and creating all test cases.

 

這道題給了我們一個數組,讓我們求這樣一個子集合,集合中的任意兩個數相互取余均為0,而且提示中說明了要使用DP來解。那么我們考慮,較小數對較大數取余一定不為0,那么問題就變成了看較大數能不能整除這個較小數。那么如果數組是無序的,處理起來就比較麻煩,所以我們首先可以先給數組排序,這樣我們每次就只要看后面的數字能否整除前面的數字。定義一個動態數組dp,其中dp[i]表示到數字nums[i]位置最大可整除的子集合的長度,還需要一個一維數組parent,來保存上一個能整除的數字的位置,兩個整型變量mx和mx_idx分別表示最大子集合的長度和起始數字的位置,我們可以從后往前遍歷數組,對於某個數字再遍歷到末尾,在這個過程中,如果nums[j]能整除nums[i], 且dp[i] < dp[j] + 1的話,更新dp[i]和parent[i],如果dp[i]大於mx了,還要更新mx和mx_idx,最后循環結束后,我們來填res數字,根據parent數組來找到每一個數字,參見代碼如下:

 

解法一:

class Solution {
public:
    vector<int> largestDivisibleSubset(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        vector<int> dp(nums.size(), 0), parent(nums.size(), 0), res;
        int mx = 0, mx_idx = 0;
        for (int i = nums.size() - 1; i >= 0; --i) {
            for (int j = i; j < nums.size(); ++j) {
                if (nums[j] % nums[i] == 0 && dp[i] < dp[j] + 1) {
                    dp[i] = dp[j] + 1;
                    parent[i] = j;
                    if (mx < dp[i]) {
                        mx = dp[i];
                        mx_idx = i;
                    }
                }
            }
        }
        for (int i = 0; i < mx; ++i) {
            res.push_back(nums[mx_idx]);
            mx_idx = parent[mx_idx];
        }
        return res;
    }
};

 

下面這種方法和上面解法的思路基本一樣,只不過dp數組現在每一項保存一個pair,相當於上面解法中的dp和parent數組揉到一起表示了,然后的不同就是下面的方法是從前往后遍歷的,每個數字又要遍歷到開頭,參見代碼如下:

 

解法二:

class Solution {
public:
    vector<int> largestDivisibleSubset(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        vector<int> res;
        vector<pair<int, int>> dp(nums.size());
        int mx = 0, mx_idx = 0;
        for (int i = 0; i < nums.size(); ++i) {
            for (int j = i; j >= 0; --j) {
                if (nums[i] % nums[j] == 0 && dp[i].first < dp[j].first + 1) {
                    dp[i].first = dp[j].first + 1;
                    dp[i].second = j;
                    if (mx < dp[i].first) {
                        mx = dp[i].first;
                        mx_idx = i;
                    }
                }
            }
        }
        for (int i = 0; i < mx; ++i) {
            res.push_back(nums[mx_idx]);
            mx_idx = dp[mx_idx].second;
        }
        return res;
    }
};

 

參考資料:

https://discuss.leetcode.com/topic/49580/c-o-n-2-solution-56ms

https://discuss.leetcode.com/topic/49456/c-solution-with-explanations/2

 

LeetCode All in One 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM