368. Largest Divisible Subset - Leetcode - SegmentFault
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 1:
nums: [1,2,3]
Result: [1,2] (of course, [1,3] will also be ok)
Example 2:
nums: [1,2,4,8]
Result: [1,2,4,8]
解答:
参考的discussion里的解法, 核心思想从小到大,每一位数都能被比他大的数整除。
Read full article from 368. Largest Divisible Subset - Leetcode - SegmentFault
No comments:
Post a Comment