forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZetBe.py
More file actions
36 lines (32 loc) Β· 1.16 KB
/
ZetBe.py
File metadata and controls
36 lines (32 loc) Β· 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'''
λ¬Έμ : μ΅λ λΆλΆ λ°°μ΄μ ν©μ ꡬνλ λ¬Έμ
νμ΄:
1. λ°°μ΄μ λͺ¨λ μκ° μμμΈ κ²½μ°, κ°μ₯ ν° μλ₯Ό λ°ν
2. λ°°μ΄μ μ²μλΆν° μμκ° λμ¬ λκΉμ§ μΈλ±μ€λ₯Ό μ΄λ
3. μμκ° λμ¨ μ΄νλΆν°λ νμ¬ ν©μ΄ μμμΈ λμμ κ³μ λνκ³ , μμκ° λλ©΄ λ€μ μμλ₯Ό μ°Ύμ
4. λ§€ λ¨κ³λ§λ€ μ΅λ ν©μ κ°±μ
μκ°λ³΅μ‘λ: O(n)
λ°°μ΄μ ν λ² μννλ©° μ΅λ λΆλΆ λ°°μ΄μ ν©μ κ³μ°νλ―λ‘ μ 체 μκ°λ³΅μ‘λλ O(n)μ΄λ€.
곡κ°λ³΅μ‘λ: O(1)
μΆκ°μ μΈ κ³΅κ°μ μ¬μ©νμ§ μμΌλ―λ‘ μ 체 곡κ°λ³΅μ‘λλ O(1)μ΄λ€.
'''
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
n = len(nums)
answ = 0
if max(nums) <= 0:
return max(nums)
i = 0
while nums[i] < 0:
i += 1
now = 0
while i < n:
if now > 0 and i < n:
now += nums[i]
i += 1
else:
while now <= 0 and i < n:
now = nums[i]
i += 1
answ = max(answ, now)
return answ