Skip to content

Commit 4c31069

Browse files
Time: 995 ms (5.04%) | Memory: 286.7 MB (6.47%) - LeetSync
1 parent 3c0e575 commit 4c31069

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed
Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1+
from functools import lru_cache
12
import math
23

34
class Solution:
4-
def maxSubArray(self, nums: List[int]) -> int:
5-
m = -math.inf
65

7-
c = 0
6+
@lru_cache(maxsize=None)
7+
def r(self, i, b):
8+
if i >= len(self.nums):
9+
return -math.inf
10+
11+
if b:
12+
return max(self.nums[i], self.nums[i] + self.r(i + 1, True))
13+
else:
14+
return max(self.r(i, True), self.r(i + 1, False))
815

9-
for num in nums:
10-
c += num
11-
m = max(m, c)
12-
c = max(c, 0)
1316

14-
return m
17+
def maxSubArray(self, nums: List[int]) -> int:
18+
self.nums = nums
19+
20+
return self.r(0, False)
21+

0 commit comments

Comments
 (0)