This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]) |