This file contains 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: | |
# return max gold sum by starting at (sr, sc) | |
def getMaxGoldFromCell(self, grid, sr, sc): | |
originalValue = grid[sr][sc] | |
bestByVisitingNeighbors = 0 | |
grid[sr][sc] = 0 | |
# explore neighbors | |
for dx, dy in ((-1, 0), (1, 0), (0, -1), (0, 1)): | |
newR = sr + dy |
This file contains 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 Heap: | |
def __init__(self, arr = None): | |
self.arr = arr or [] | |
def __bool__(self): | |
return bool(self.arr) | |
def __getitem__(self, key): | |
return self.arr[key] | |
def bubbleup(self, idx): | |
# index at root node. Cannot bubble up any further | |
if idx == 0: |
This file contains 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
from collections import defaultdict | |
class Range: | |
def __init__(self): | |
self.start = None | |
self.end = None | |
self.char = None | |
def __repr__(self): | |
return '(' + self.char + ':' + str(self.start) + '->' + str(self.end) + ')' | |
class Solution: | |
def partitionLabels(self, S: str) -> List[int]: |
This file contains 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 getMax(self, arr, k, idx, cache): | |
# I am partitioning 1 element | |
if idx == len(arr) - 1: | |
return arr[idx] | |
# I need to get max sum after partitioning | |
maxSum = 0 | |
# [1,2,9,30] | |
# I need to try partitioning from 1 -> K elements |
This file contains 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
from collections import defaultdict | |
import heapq | |
class Solution: | |
def maxProbability(self, n: int, edges: List[List[int]], succProb: List[float], start: int, end: int) -> float: | |
''' | |
Input | |
n - number of nodes | |
edges - (u,v) for an undirected edge. | |
succProb - edge[i]'s weight | |
start - start node |
This file contains 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
from collections import deque | |
class MaxMonoQueue: | |
# Invariant: all preceeding numbers that are < newVal need to get out! | |
def __init__(self): | |
self.queue = deque() | |
def enqueue(self, val, idx): | |
# maintaining the invariant | |
while self.queue and self.queue[-1][0] <= val: | |
self.queue.pop() | |
self.queue.append((val, idx)) |
This file contains 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
# Definition for a binary tree node. | |
# class TreeNode: | |
# def __init__(self, val=0, left=None, right=None): | |
# self.val = val | |
# self.left = left | |
# self.right = right | |
class Solution: | |
def generateTrees(self, N, treeList): | |
# for all trees: | |
# treeList[N - 1].append(tree) |
This file contains 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 backtrack(self, n, k, partial): | |
# Base Case | |
# If I have generated a string of the correct length | |
# do not proceed any longer. | |
if len(partial) == n: | |
self.counter += 1 | |
# If I have found the 'k'th happy string, Return this to the main caller. | |
if self.counter == k: |
This file contains 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: | |
# This function updates the lowLink of nodeNum. | |
def dfs(self, at, parent, bridges): | |
# Initialize current node | |
self.visited[at] = True | |
self.lowLink[at] = self.rankNum | |
self.ids[at] = self.rankNum | |
self.rankNum += 1 | |
This file contains 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
{ | |
"licenseKey": "552258f2-1f24-43f9-914a-de3c4ff21b88", | |
"devtools_port": 9090, | |
"runtime": { | |
"arguments": "", | |
"version": "18.87.55.19", | |
"fallbackVersion": "", | |
"futureVersion": "" | |
}, | |
"platform": { |