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
This discussion was converted from issue #57 on September 15, 2026 11:03.
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.
Problem link
https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/
Problem Summary
의자와 식물이 있을 때 의자를 2개씩 묶을 수 있는 경우의 수를 구하는 문제.
Solution
딱 보니 DP 냄새가 나서 탑 다운으로 풀었는데 ... 메모리 초과가 난다.
바텀업으로 고치니 통과!
또, 2차원 DP를 1차원으로 줄이니 더 빨라지고 메모리도 적게 먹는다.
간단하게 설명하자면
로 정의하고 점화식을 만들 수 있다. 그리고 for문을 돌면서 덮어 씌워지므로 1차원으로 줄일 수 있다.
추가로 수학적인 풀이도 존재하는데, 의자는 무조건 2개씩 붙여야 하므로 경우의 수는 식물이 메인이 되고 의자가 2개씩 나온 사이의 식물들이 모여져 있는 카운트를 센 다음 다 곱하면 최종 경우의 수가 된다. 아래 디스커션 참고.
https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/discuss/1709704/Greedy-Solution-or-C%2B%2B
Source Code
All reactions