Skip to content

Commit 9bd970e

Browse files
committed
[Leo] 12th Week solutions (4,5)
1 parent f4050e3 commit 9bd970e

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

jump-game/Leo.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def canJump(self, nums: List[int]) -> bool:
3+
target = len(nums) - 1
4+
5+
for i in range(target, -1, -1):
6+
if i + nums[i] >= target:
7+
target = i
8+
9+
return True if target == 0 else False
10+
11+
# maxReach = 0
12+
# for i, jump in enumerate(nums):
13+
# if i > maxReach:
14+
# return False
15+
# maxReach = max(maxReach, i + jump)
16+
# return True
17+
18+
## Both TC: O(n), SC: O(1)

maximum-subarray/Leo.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def maxSubArray(self, nums: List[int]) -> int:
3+
4+
maxSub = nums[0]
5+
curSum = 0
6+
7+
for n in nums:
8+
if curSum < 0:
9+
curSum = 0
10+
11+
curSum += n
12+
maxSub = max(curSum, maxSub)
13+
14+
return maxSub
15+
16+
## TC: O(n), SC: O(1)

0 commit comments

Comments
 (0)