zoukankan      html  css  js  c++  java
  • 【leetcode题目整理】数组中找子集

    368. Largest Divisible Subset

    题意:找到所有元素都不同的数组中满足以下规则的最大子集,规则为:子集中的任意两个元素a和b,满足a%b=0或者b%a=0。

    解答:利用动态规划法求解。先给数组排好序。定义dp[i]表示以nums[i]结尾的子集的大小,则dp[i+1]=dp[j]+1,if nums[i+1]%nums[j]==0, 0<=j<=i,或者dp[i+1]=1。时间复杂度为O(n2)。

    代码如下:

     1 public List<Integer> largestDivisibleSubset(int[] nums) {
     2         Arrays.sort(nums);
     3         int n = nums.length;
     4         List<Integer> res = new ArrayList<>();
     5         if(n==0) return res;
     6         int[] dp = new int[n];
     7         int[] trace = new int[n];
     8         Arrays.fill(dp, 1);
     9         trace[0]=-1;
    10         int max=0, maxIndex=0;
    11         for(int i=1; i<n; i++){
    12             for(int j=0; j<i; j++){
    13                 if(nums[i]%nums[j]==0){
    14                     if(dp[i]<dp[j]+1){
    15                         dp[i]=dp[j]+1;
    16                         trace[i]=j;
    17                     }
    18                 }
    19             }
    20             if(dp[i]>max)
    21             {
    22                 maxIndex=i;
    23                 max=dp[i];
    24             }
    25         }
    26 //        print(dp);
    27 //        System.out.println();
    28 //        print(trace);
    29 //        System.out.println();
    30         //res.insert(0, nums[maxIndex]);
    31         while(maxIndex>=0 && dp[maxIndex]>1){
    32             res.add(0, nums[maxIndex]);
    33             maxIndex=trace[maxIndex];
    34         }
    35         res.add(0, nums[maxIndex]);
    36         return res;
    37     }
  • 相关阅读:
    centos安装pip
    centos修改国内镜像源
    centos配置snmp服务
    django使用ModelForm上传文件
    Vue slot
    umi3.2+ targets ie不生效的问题
    mongo环境快速搭建工具 mlaunch
    mac上常用软件
    磁盘性能测试工具 iozone
    磁盘性能测试工具 bonnie++
  • 原文地址:https://www.cnblogs.com/shizhh/p/5692978.html
Copyright © 2011-2022 走看看