• largest-divisible-subset


    用了两种方法:

    class Solution {
        // size of vector -> set of last item of vector
        map<int, set<int>> size_mp;
        // last item of vector -> whole vector
        map<int, vector<int>> result_mp;
        
    public:
        // if new item can divide last item, it can divide all items in vector,
        // so that no need to check previous item
        vector<int> largestDivisibleSubset(vector<int>& nums) {
            if (nums.size() < 1) {
                return nums;
            }
            
            sort(nums.begin(), nums.end());
            
            for (vector<int>::iterator itr = nums.begin();
                 itr != nums.end(); ++itr) {
            
                // only need prev item to get whole items from result_mp
                int prev = -1;
                
                // iterator from longer vector to shorter
                for (map<int, set<int>>::reverse_iterator ritr = size_mp.rbegin();
                     ritr != size_mp.rend(); ++ritr) {
                
                    for (set<int>::iterator sitr = ritr->second.begin();
                         sitr != ritr->second.end(); ++sitr) {
                    
                        // if last item of long vector can divide new number,
                        // no need to check other vector or previous item
                        if (*itr % *sitr == 0) {
                            prev = *sitr;
                            break;
                        }
                    }
                    if (prev != -1) {
                        break;
                    }
                }
                
                vector<int> tmp_vec;
                if (prev != -1) {
                    tmp_vec = result_mp[prev];
                }
                tmp_vec.push_back(*itr);
                result_mp[*itr] = tmp_vec;
                int newsize = tmp_vec.size();
                
                if (size_mp.find(newsize) == size_mp.end()) {
                    set<int> tmp_set;
                    tmp_set.insert(*itr);
                    size_mp[newsize] = tmp_set;
                }
                else {
                    size_mp[newsize].insert(*itr);
                }
                
            }
            
            set<int>::iterator sitr_new = size_mp.rbegin()->second.begin();
            return result_mp[*sitr_new];
            
        }
        
    };
    31 / 31 test cases passed.
    Status: 

    Accepted

    Runtime: 261 ms

    开始用的下面这种方法,超时了:

    class Solution {
        vector<int> nums;
        int nlen;
        vector<int> result;
        
        void func(vector<int>& iv, int index) {
            int vlen = iv.size();
            int divisor = 1;
            if (vlen > 0) {
                divisor = iv[vlen-1];
            }
            
            for (int k=index; k<nlen; ++k) {
                if (nums[k] % divisor != 0) {
                    continue;
                }
                // without nums[k]
                func(iv, k+1);
                // with nums[k]
                iv.push_back(nums[k]);
                func(iv, k+1);
                iv.pop_back();
                break;
            }
            
            if (iv.size() > result.size()) {
                result = iv;
            }
        }
        
    public:
        vector<int> largestDivisibleSubset(vector<int>& arg) {
            nums = arg;
            sort(nums.begin(), nums.end());
            nlen = nums.size();
            if (nlen <= 1) {
                return nums;
            }
            
            vector<int> tmp;
            func(tmp, 0);
            return result;
            
        }
        
    };
  • 相关阅读:
    组合数
    2019牛客暑期多校训练营第一场 E ABBA
    2019牛客暑期多校训练营(第一场) H XOR
    POJ-1742Coins
    POJ 1384 Piggy-Bank
    operator.itemgetter函数
    常用内置模块之itertools
    解决Django REST framework中的跨域问题
    关于模板渲染冲突的问题
    什么是跨域?以及如何解决跨域问题?
  • 原文地址:https://www.cnblogs.com/charlesblc/p/5623319.html
Copyright © 2020-2023  润新知