LeetCode 368 Largest Divisible Subset

LeetCode 368 Largest Divisible Subset Problem

Download Code
class Solution(object):
    # def largestDivisibleSubset(self, nums):
    #     """
    #     :type nums: List[int]
    #     :rtype: List[int]
    #     """
    #     # https://discuss.leetcode.com/topic/49455/4-lines-in-python
    #     S = {-1: set()}
    #     for x in sorted(nums):
    #         # S[x] is the largest subset with x as the largest element
    #         S[x] = max((S[d] for d in S if x % d == 0), key=len) | {x}
    #     return list(max(S.values(), key=len))

    def largestDivisibleSubset(self, nums):
        ls = len(nums)
        S = {-1: set()}
        for num in sorted(nums):
            candicate = []
            for key in S:
                if num % key == 0:
                    candicate.append(S[key])
            # max previous with curr
            S[num] = max(candicate, key=len) | {num}
        return 
Download Largest Divisible Subset.py

List of all Largest Divisible Subset problems

Leetcode 368 Largest Divisible Subset problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.

Feedback is the most important part of any website.

If you have any query, suggestion or feedback, Please feel free to contact us.