Skip to content

Commit 7f8423b

Browse files
committed
add solution: longest-increasing-subsequence
1 parent a490985 commit 7f8423b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'''
2+
๋ฌธ์ œ: ์ตœ์žฅ ์ฆ๊ฐ€ ๋ถ€๋ถ„ ์ˆ˜์—ด
3+
ํ’€์ด: ์ด๋ถ„ ํƒ์ƒ‰์„ ํ™œ์šฉํ•˜์—ฌ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆ˜์—ด์„ ์œ ์ง€ํ•˜๋ฉฐ ๊ธธ์ด๋ฅผ ๊ณ„์‚ฐ, ์ƒˆ๋กœ์šด ์ˆ˜๊ฐ€ ๊ธฐ์กด ์ˆ˜์—ด์˜ ๋งˆ์ง€๋ง‰ ์ˆ˜๋ณด๋‹ค ํฌ๋ฉด ์ถ”๊ฐ€, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ด๋ถ„ ํƒ์ƒ‰์œผ๋กœ ์ ์ ˆํ•œ ์œ„์น˜๋ฅผ ์ฐพ์•„ ๊ต์ฒด
4+
์‹œ๊ฐ„๋ณต์žก๋„: O(n log n)
5+
๊ณต๊ฐ„๋ณต์žก๋„: O(n)
6+
์‚ฌ์šฉํ•œ ์ž๋ฃŒ๊ตฌ์กฐ: ๋ฆฌ์ŠคํŠธ
7+
'''
8+
9+
10+
from bisect import bisect_left
11+
12+
class Solution:
13+
def lengthOfLIS(self, nums: List[int]) -> int:
14+
answer = [nums[0]]
15+
for i in range(1, len(nums)):
16+
if answer[-1] < nums[i]:
17+
answer.append(nums[i])
18+
else:
19+
now = bisect_left(answer, nums[i])
20+
answer[now] = nums[i]
21+
22+
return len(answer)
23+
24+
25+

0 commit comments

Comments
ย (0)