Java O(n^2). Nested loops


  • -1
    Q
    public List<Integer> largestDivisibleSubset(int[] nums) {
           List<Integer> largest = new ArrayList<>();
           Arrays.sort(nums);
           for (int i=nums.length-1;i>=0;i--){
        	   List<Integer> l = new ArrayList<>();
        	   l.add(nums[i]);
        	   for (int j=i-1;j>=0;j--)
        		   if (l.get(l.size()-1)%nums[j]==0)
        			   l.add(nums[j]);
        	   if (l.size()>largest.size())
        		   largest = l;
        	   
           }
           return largest;
        }

  • 0
    S

    @qgambit2 wrong answer for "[1,2,4,8,9,72]"


Log in to reply
 

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