Skip to content

Commit b178190

Browse files
author
wuduhren
committed
updates
1 parent 7568868 commit b178190

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

problems/python3/gas-station.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
First thing, you must understand that if sum(gas)>=sum(cost) there must be an answer. Guaranteed.
3+
If we know there is an answer, we can simply test all the index.
4+
If the currGas ever drops to below 0, it means that we need to switch a "start".
5+
6+
Time: O(N)
7+
Space: O(1)
8+
"""
9+
class Solution:
10+
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
11+
if sum(gas)<sum(cost): return -1
12+
13+
start = 0
14+
currGas = 0
15+
16+
for i in range(len(gas)):
17+
currGas += gas[i]-cost[i]
18+
19+
if currGas<0:
20+
currGas = 0
21+
start = i+1
22+
23+
return start

problems/python3/jump-game-ii.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
l and r is the index range we can reach within "steps".
3+
So while r can not reach the end (`r<len(nums)-1`), we keep update l, r and steps.
4+
5+
Time: O(N)
6+
Space: O(1)
7+
"""
8+
class Solution:
9+
def jump(self, nums: List[int]) -> int:
10+
l = r = 0
11+
steps = 0
12+
13+
while r<len(nums)-1:
14+
farest = float('-inf')
15+
for i in range(l, r+1):
16+
farest = max(farest, i+nums[i])
17+
18+
l = r+1
19+
r = farest
20+
steps += 1
21+
22+
return steps

problems/python3/jump-game.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def canJump(self, nums: List[int]) -> bool:
3+
maxIndex = 0
4+
5+
for i, num in enumerate(nums):
6+
if maxIndex<i:
7+
return False
8+
maxIndex = max(maxIndex, i+num)
9+
10+
return True

problems/python3/maximum-subarray.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
Time: O(N)
3+
Space: O(1)
4+
5+
Calculate the prefix sum. Whenever the prefix is negative, we ignore all the value before.
6+
Update the ans along the way.
7+
"""
8+
class Solution:
9+
def maxSubArray(self, nums: List[int]) -> int:
10+
ans = float('-inf')
11+
currSum = 0
12+
13+
for num in nums:
14+
if currSum<0: currSum = 0
15+
currSum += num
16+
ans = max(ans, currSum)
17+
return ans

problems/python3/median-of-two-sorted-arrays.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
"""
2+
Try to find a i on array A and corespoding j on array B.
3+
4+
Aleft = A[i]
5+
Aright = A[i+1]
6+
Bleft = B[j]
7+
Bright = B[j+1]
8+
9+
Such that Aleft<=Bright and Bleft<=Aright
10+
11+
This means that A[:i+1] and B[:j+1] holds all the elements that is less than the median.
12+
Aright A[i+1:] and B[j+1:] holds all the elements that is larger or equal to the median.
13+
"""
114
class Solution:
215
def findMedianSortedArrays(self, A: List[int], B: List[int]) -> float:
316
if len(A)>len(B): A, B = B, A

0 commit comments

Comments
 (0)