C++ O(n^2) solution


  • 0
    M
    vector<int> res;
            if(nums.size() == 0)
                return res;
            int n = nums.size();
            sort(nums.begin(), nums.end());
            vector<vector<int>> temp(n);
            temp[0].push_back(nums[0]);
            for(int i=1; i<n; i++){
                int count = 0;
                for(int j=i-1; j>=0; j--){
                    if(nums[i]%nums[j] == 0){
                        temp[i].insert(temp[i].begin(), temp[j].begin(), temp[j].end());
                        temp[i].push_back(nums[i]);
                        break;
                    }
                    count++;
                }
                if(count == i)
                    temp[i].push_back(nums[i]);
            }
            int a = 0, b = 0;
            for(int i=0; i<temp.size(); i++){
                if(temp[i].size() > a){
                    a = temp[i].size();
                    b = i;
                }   
            }
            return temp[b];

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.