Skip to content

Instantly share code, notes, and snippets.

class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
for i in range(2, len(cost)):
cost[i] += min(cost[i - 1],cost[i - 2])
return min(cost[-1], cost[-2])
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
minCost = [0] * (len(cost) + 1)
minCost[0] = minCost[1] = 0
for index in range(2, len(cost) + 1):
minCost[index] = min(minCost[index-1] + cost[index-1], minCost[index-2] + cost[index-2])
# print(index, minCost[index])
return minCost[len(cost)]
class Solution:
def canJump(self, nums: List[int]) -> bool:
far = 0
for index in range(0, len(nums)):
if index > far: return False
currentFar = nums[index] + index
far = max(far, currentFar)
if far + 1 >= len(nums): return True
return False
class Solution:
def canJump(self, nums: List[int]) -> bool:
def dfsVisit(nums, currentIndex, hasVisited) -> bool:
maxStep = nums[currentIndex]
# print(currentIndex, maxStep)
if maxStep == 0: return False
hasVisited[currentIndex] = True
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
if len(nums) == 0: return 0
maxValue = nums[0]
sum = nums[0]
for index in range(1, len(nums)):
sum = max(sum + nums[index], nums[index])
maxValue = max(sum, maxValue)
return maxValue
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
if len(nums) == 0: return 0
maxValue = nums[0]
sum = nums[0]
for index in range(1, len(nums)):
if nums[index] > sum and sum < 0:
sum = nums[index]
else:
sum += nums[index]
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if len(prices) < 2: return 0
profit = 0
buy = prices[0]
for price in prices[1:]:
buy = min(buy, price)
profit = max(profit, price - buy)
return profit
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if len(prices) <= 1: return 0
buy = prices[0]
sell = 0
maxProfit = 0
for index in range(1, len(prices)):
if buy == prices[index]: continue
if prices[index] < buy:
buy = prices[index]
class Solution:
def maxProfit(self, prices: List[int]) -> int:
maxProfit = 0
for buyIndex in range(0, len(prices) - 1):
for sellIndex in range(buyIndex + 1, len(prices)):
if prices[buyIndex] > prices[sellIndex]: continue
profit = prices[sellIndex] - prices[buyIndex]
maxProfit = maxProfit if maxProfit > profit else profit
return maxProfit
class Solution(object):
def isValidSudoku(self, board):
for row in range(0, 9):
# col, row
listCol = [0] * 10
listRow = [0] * 10
for col in range(0, 9):
valueCol = 0 if board[row][col] == '.' else int(board[row][col])