forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsungjinwi.py
More file actions
23 lines (22 loc) ยท 885 Bytes
/
sungjinwi.py
File metadata and controls
23 lines (22 loc) ยท 885 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""
/ํ์ด ๋ด๋ ์ ์ดํด ๋ชปํด์ ์ถ๊ฐ ์ฝ๋ฉํธ/
nums[i]๊ฐ ๊ทธ ์ ๊น์ง subarray์ ํฉ total๋ณด๋ค ์์ ์์์ธ ์ผ์ด์ค๋ ์ด๋ป๊ฒ ๋๋๊ฑฐ์ง ๊ณ ๋ฏผํ๋๋ฐ
ex) total : -1, nums[i] = -2
์ด์ฐจํผ -1์ธ ์์ ์ maxTotal์ด ์
๋ฐ์ดํธ ๋์ผ๋ฏ๋ก total์ nums[i]๋ถํฐ ๋ํ๊ธฐ ์์ํ๋ค๋ ์๋ฏธ๋ก -2๋ก ์ค์ ํ๋ค๋ ๊ฒ์ ๊นจ๋ฌ์
๋ฐ๋ผ์ ์ด์ ๊น์ง subarray์ ํฉ๋ง ์์ ์์ ์ฒดํฌ
TC : for๋ฌธ ํ๋ฒ
=> O(N)
SC : ์ถ๊ฐ์ ์ธ ๋ฐฐ์ด ๋ฑ ๋ฉ๋ชจ๋ฆฌ ์ฐ์ง ์์ผ๋ฏ๋ก
=> O(1)
"""
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
total = nums[0]
maxTotal = nums[0]
for i in range(1, len(nums)) :
if (total < 0) :
total = nums[i]
else :
total += nums[i]
maxTotal = max(total, maxTotal)
return (maxTotal)