zoukankan      html  css  js  c++  java
  • [LeetCode 368] 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

    Given nums = [1,2,3], return [1,2] or [1,3]

    Given nums = [1,2,4,8], return [1,2,4,8]

     

    Clarification: If there is no such pair, then a single integer should be returned. For example, nums = [3, 5, 7], then 3 or 5 or 7 should be returned as the only integer in the subset.

     

    Incorrect greedy approach: sort the input array first, then we process from the largest to the smallest number and keep all collections of all-pair-divisible. For a number, check all the smallest number of existing collections and pick the maximum smallest number collection to add to. However, this greedy algorithm is incorrect. Consider this counter example [4, 8, 10, 240]. 

    Following the greedy approach, we'll have [10, 240] and [4, 8] as the final subsets. The correct answer is [4, 8, 240] of size 3. Greedily adding 10 to [240] is wrong!

    When greedy does not work, we should consider dynamic programming.

    Algorithm

    1. Sort the given array in ascending order.

    2. Use dynamic programming as follows to get one such largest subset.

    State: count[i] is the number of integers of the subset that satisifies the divisible condition. nums[i] is the largest integer of this subset.

    Function: count[i] = {max(count[j]), for all j from 0 to i - 1} + 1 if nums[i] % nums[j] == 0;  count[i] = 0, if no such j meets the divisible condition.

    Initialization: count[i] = 1.

    3. Fill in all values of count[]. 

    4. Find the max value in count[] and its index idx.

    5. Starting from nums[idx] and traverse nums backwards, adding all numbers that belong in the result subset.

     

     1 public class Solution {
     2     /**
     3      * @param nums a set of distinct positive integers
     4      * @return the largest subset 
     5      */
     6     public List<Integer> largestDivisibleSubset(int[] nums) {
     7         List<Integer> result = new ArrayList<Integer>();
     8         if(nums == null || nums.length <= 1){
     9             return result;
    10         }
    11         int n = nums.length;
    12         Arrays.sort(nums);
    13         int[] count = new int[n];
    14         for(int i = 0; i < n; i++){
    15             count[i] = 1;
    16         }
    17         for(int i = 1; i < n; i++){
    18             int maxNum = 0;
    19             for(int j = 0; j < i; j++){
    20                 if(nums[i] % nums[j] == 0){
    21                     maxNum = Math.max(maxNum, count[j]);
    22                 }
    23             }
    24             count[i] = maxNum + 1;
    25         }
    26         int maxNum = Integer.MIN_VALUE;
    27         int idx = 0;
    28         for(int i = 0; i < n; i++){
    29             if(count[i] > maxNum){
    30                 maxNum = count[i];
    31                 idx = i;
    32             }
    33         }
    34         int val = nums[idx];
    35         result.add(val);
    36         for(int i = idx - 1; i >= 0; i--){
    37             if(val % nums[i] == 0){
    38                 result.add(nums[i]);
    39                 val = nums[i];
    40             }
    41         }
    42         return result;
    43     }
    44 }

     

     

     

    Related Problems

    Longest Increasing Subsequence 

  • 相关阅读:
    学习英文之社区,博客及源码 转载
    Windows 的 80 端口被 System 进程占用解决方案
    MySQL 报错:Translating SQLException with SQL state '42000', error code '1064', message
    程序员如何写工作日志?
    log4j 配置文件参数说明
    Java 后端彻底解决跨域问题(CORS)
    常用正则表达式语法
    使用 windows 批处理指令(BAT文件)进行压缩文件(zip)解压操作
    使用 windows 批处理指令(BAT文件)进行文件删除、复制操作
    idea 关于高亮显示与选中字符串相同的内容踩过的坑
  • 原文地址:https://www.cnblogs.com/lz87/p/7498469.html
Copyright © 2011-2022 走看看