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
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-sets-of-k-non-overlapping-line-segments/
Problem Summary
1차원 위에 놓인 점 n개에서 서로 겹치지 않는 선분 k개를 그리는 경우의 수를 구하는 문제. 선분은 점을 둘 이상 덮어야 하고 끝점은 공유할 수 있다.
Solution
modular 나온 걸 보고 딱 DP겠거니 했다.
어제 2472처럼 겹치지 않게 k개를 고르는 문제인데 이번엔 최대 개수가 아니라 경우의 수다. 한 점에서 할 수 있는 건 이어붙이기, 끊고 넘어가기, 새로 시작하기 세 가지인데 이어붙이는 중인지를 상태로 넣어야 해서 3차원이 된다.
끝점을 공유할 수 있으니
c == 1에서 끊자마자 새로 시작하는solve(i+1, j+1, 1)이 따로 필요하다.j == k이고c == 0이면 남은 점은 그냥 두면 되니까 1을 돌려주면 된다.처음엔 또 편하게
@cache로 짰다가 메모리 초과... 항목이 100만 개인데 인자 3개짜리 캐시는 항목당 130바이트 정도라 캐시만 125MB다. 3차원 배열로 바꾸니 통과.시간복잡도는 O(nk)
참고로 수학적으로는
C(n + k - 1, 2k)로 바로 나온다. stars and bars로 증명된다. 자세한건 에디토리얼 참고.Source Code
All reactions