You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
리스트가 주어질 때 모든 subset이 각각의 배수가 되도록 하는 최대 길이의 subset을 구하는 문제.
Solution
일단 순서가 중요하지 않으므로 정렬을 먼저 하자.
그러면 뭔가 보이는데 두 인덱스를 i, j (i < j) 라 했을 때 nums[j] % nums[i] == 0이면 뒤로 이어 붙일 수 있다. 이런식으로 모든 원소에 대해 길다면 이어 붙이는 식으로 이어 나갈 수 있다. LIS (Longest Increasing Subsequence) 문제와 유사하다.
구현은 간단하게 O(n^2) Top Down DP로 해봤는데 메모리도 O(n^2) 먹어서... (크지는 않지만) 메모리도 O(n) 하려면 길이 배열과 parent 배열을 둘다 저장하면 가능하다.
구현이 쉽기 위해 앞에 더미로 1을 더 집어넣었다. (가장 긴 리스트가 0번부터 시작하지 않을 수 있기 때문)
This discussion was converted from issue #87 on September 15, 2026 11:06.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Problem link
https://leetcode.com/problems/largest-divisible-subset
Problem Summary
리스트가 주어질 때 모든 subset이 각각의 배수가 되도록 하는 최대 길이의 subset을 구하는 문제.
Solution
일단 순서가 중요하지 않으므로 정렬을 먼저 하자.
그러면 뭔가 보이는데 두 인덱스를 i, j (i < j) 라 했을 때
nums[j] % nums[i] == 0이면 뒤로 이어 붙일 수 있다. 이런식으로 모든 원소에 대해 길다면 이어 붙이는 식으로 이어 나갈 수 있다.LIS (Longest Increasing Subsequence) 문제와 유사하다.
구현은 간단하게 O(n^2) Top Down DP로 해봤는데 메모리도 O(n^2) 먹어서... (크지는 않지만) 메모리도 O(n) 하려면 길이 배열과 parent 배열을 둘다 저장하면 가능하다.
구현이 쉽기 위해 앞에 더미로 1을 더 집어넣었다. (가장 긴 리스트가 0번부터 시작하지 않을 수 있기 때문)
Source Code
All reactions