forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathliza0525.py
More file actions
19 lines (16 loc) ยท 911 Bytes
/
liza0525.py
File metadata and controls
19 lines (16 loc) ยท 911 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 7๊ธฐ ํ์ด
# ์๊ฐ ๋ณต์ก๋: O(n)
# - nums์ ๋ชจ๋ ์์๋ฅผ ํ์ํ๊ธฐ ๋๋ฌธ์ nums์ ๊ธธ์ด์ ๊ด๋ จ๋์ด ์์
# ๊ณต๊ฐ ๋ณต์ก๋: O(1)
# - ๋ณ์ ์ด์ธ์ ๊ฐ์ฒด๋ฅผ ์ฐ์ง ์์๊ธฐ ๋๋ฌธ์ ๊ณต๊ฐ ๋ณต์ก๋๋ O(1)์ด๋ค(input ๊ธธ์ด์ ์๊ด ์์)
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
curr_sum = nums[0]
max_sum = nums[0]
for i in range(1, len(nums)):
# 1. (ํ์ฌ๊น์ง์ ํฉ๊ณผ i์ ์์๋ฅผ ๋ํ ๊ฐ)๊ณผ i์ ์์๋ฅผ ์๋ก ๋น๊ตํ์ฌ ์ง๊ธ๊น์ง์ ํฉ์ ์
๋ฐ์ดํธํ๋ค.
# - ์๊ธฐ ์์ ๋ง์ผ๋ก๋ ์ด์ ํฉ๋ค๋ณด๋ค ํฌ๋ค๋ฉด ์ด์ ํฉ๋ค์ ์๋ฏธ๊ฐ ์์ด์ง์ ์๊ธฐ
curr_sum = max(nums[i], curr_sum + nums[i])
# ํ์ฌ๊น์ง์ ํฉ๊ณผ ์ด์ max ํฉ์ ๋น๊ตํ์ฌ ์
๋ฐ์ดํธ
max_sum = max(max_sum, curr_sum)
return max_sum