forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChaedie.py
More file actions
64 lines (50 loc) · 1.4 KB
/
Chaedie.py
File metadata and controls
64 lines (50 loc) · 1.4 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
Solution1:
sliding window 일 것 같지만 구현이 어려워 일단 Brute Force 부터 진행합니다.
-> 시간 초과로 실패
Time: O(n^2) = n(for) * n(for)
Space: O(n) = cur 배열
"""
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
n = len(nums)
max_sum = float(-inf)
for i in range(n):
cur = []
cur_sum = 0
for j in range(i, n):
cur_sum += nums[j]
cur.append(nums[j])
max_sum = max(max_sum, cur_sum)
return max_sum
"""
Solution2:
Sliding Window로 풀 수 있을거라 생각했는데 잘 안되었습니다.
"""
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
n = len(nums)
max_sum = float(-inf)
l = 0
window = 0
for r in range(n):
window += nums[r]
max_sum = max(max_sum, window)
while max_sum < window:
l += 1
return max_sum
"""
Solution3 - 알고달레:
솔루션을 통해 학습했습니다.
이해가 어려워 다시 풀어볼 예정입니다.
Time: O(n)
Space: O(1)
"""
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
max_sum = nums[0]
cur_sum = 0
for num in nums:
cur_sum = max(cur_sum + num, num)
max_sum = max(cur_sum, max_sum)
return max_sum