Skip to content

Commit d31ada6

Browse files
committed
longest-increasing-subsequence solution
1 parent e3571de commit d31ada6

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def lengthOfLIS(self, nums: List[int]) -> int:
3+
'''
4+
๋ชจ๋ฅด๊ฒ ์–ด์„œ ํ’€์ด๋ดค์Šต๋‹ˆ๋‹ค ใ…œใ…œ
5+
1.problem: ์ฆ๊ฐ€ํ•˜๋Š” ๊ฐ€์žฅ ๊ธด subsequence length return (์ตœ์žฅ ์ฆ๊ฐ€ ๋ถ€๋ถ„ ์ˆ˜์—ด)
6+
2.์กฐ๊ฑด
7+
- nums array ๊ธธ์ด ์ตœ์†Œ 1 , ์ตœ๋Œ€ 2500
8+
- ์›์†Œ ๊ฐ’ ์Œ์ˆ˜ ๊ฐ€๋Šฅ
9+
3.ํ’€์ด
10+
- dp : time complexity O(n^2)
11+
dp[i] = i๋ฒˆ์งธ ์›์†Œ๋ฅผ ๋งˆ์ง€๋ง‰์œผ๋กœํ•˜๋Š” LIS
12+
dp[i] max(dp[i], dp[j]+1) ๋‚˜ ๋ณด๋‹ค ์ž‘์€ ์• ๋“ค ์ค‘ ๊ฐ€์žฅ ๊ธด LIS + 1
13+
'''
14+
15+
n = len(nums)
16+
dp = [1] * (n)
17+
18+
for i in range(n):
19+
for j in range(i):
20+
#์•ž ์ˆซ์ž nums[j]๊ฐ€ ์ง€๊ธˆ nums[i]๋ณด๋‹ค ๋” ์ž‘์€ ๊ฒฝ์šฐ dp[i] update
21+
if nums[j] < nums[i]:
22+
dp[i] = max(dp[i], dp[j] + 1)
23+
return max(dp)
24+
25+
26+

0 commit comments

Comments
ย (0)