File tree Expand file tree Collapse file tree
longest-increasing-subsequence Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # 7๊ธฐ ํ์ด
2+ # ์๊ฐ ๋ณต์ก๋: O(n^2)
3+ # - ๊ฐ ์์๋ง๋ค ์ด์ ์ธ๋ฑ์ค๋ค์ ๋ชจ๋ ํ์ธํ๋ฏ๋ก ์ด์ค for๋ฌธ์ ๋์์ผ ํจ
4+ # ๊ณต๊ฐ ๋ณต์ก๋: O(n)
5+ # - dp ๊ณ์ฐ์ ์ํ ๋ฆฌ์คํธ๋ nums์ ๊ธธ์ด์ ์์กด
6+ class Solution :
7+ def lengthOfLIS (self , nums : List [int ]) -> int :
8+ # ๊ฐ ์ธ๋ฑ์ค์์์ ์ต๋ ๊ธธ์ด๋ฅผ ์ ์ฅํ๋ dp ๋ฆฌ์คํธ
9+ dp = [1 for _ in range (len (nums ))]
10+
11+ for i in range (1 , len (nums )):
12+ # i ์ธ๋ฑ์ค๊น์ง์ ์ต๋ ๊ธธ์ด๋ฅผ ๊ณ์ฐ ํ๊ธฐ ์ํด
13+ # ์ด์ ์ธ๋ฑ์ค๋ค ์ค ๊ฐ์ฅ ๊ธด ๊ธธ์ด๋ฅผ ์ฐพ์๋ด์ด ์ ์ฅํ๋ ์ฅ์์ด๋ค.
14+ max_lis = 1
15+ for j in range (0 , i ):
16+ # j ์ธ๋ฑ์ค์ ์๋ฏธ๋ i๋ณด๋ค ์ด์ ์ธ๋ฑ์ค ํ๋ฐฉํ๊ธฐ ์ํจ
17+ if nums [j ] < nums [i ]: # ์ด์ ์ธ๋ฑ์ค(j ์ธ๋ฑ์ค)์ ์๋ ๊ฐ์ด i ์ธ๋ฑ์ค์ ๊ฐ๋ณด๋ค ์๋ค๋ฉด
18+ # ๊ธฐ์กด์ max_lis์ j ์ธ๋ฑ์ค๊น์ง์ ์ต๋ ๊ธธ์ด ๊ฐ + 1(i ์ธ๋ฑ์ค ํฌํจํ ๊ธธ์ด)๋ฅผ ๋น๊ตํ์ฌ
19+ # ๋ ํฐ ์๋ฅผ max_list์ ์
๋ฐ์ดํธ ํด์ค๋ค.
20+ max_lis = max (max_lis , dp [j ] + 1 )
21+
22+ # ๊ณ์ฐ๋ max_lis๋ฅผ dp[i]์ ์
๋ฐ์ดํธ
23+ dp [i ] = max_lis
24+
25+ return max (dp )
You canโt perform that action at this time.
0 commit comments