my AC code is definitely wrong not sure why pass the OJ


  • 0
    Z

    below is my solution, the last loop to get the result is wrong, but it pass the OJ strange
    public List<Integer> largestDivisibleSubset(int[] nums) {
    if(nums.length==0) return new ArrayList<>();
    Arrays.sort(nums);
    int[] dp = new int[nums.length];
    Arrays.fill(dp, 1);
    List<Integer> res = new ArrayList<>();
    int i=nums.length-1;
    while(i>=0)
    {
    for(int j=i-1; j>=0; j--)
    {
    if(nums[i]%nums[j]==0)
    dp[j]=Math.max(dp[j], dp[i]+1);
    }
    i--;
    }

        for(int k=nums.length-1; k>=0; k--)
        {
            if(res.size()<dp[k])
                res.add(0, nums[k]);
        }
        
        return res;
        
    }

Log in to reply
 

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