Skip to content

Instantly share code, notes, and snippets.

View RP-3's full-sized avatar

Rohan Rogers RP-3

  • San Francisco Bay Area
View GitHub Profile
# Bottom-up DFS. Accepted.
# O(n)
class Solution:
def maxProduct(self, root: TreeNode) -> int:
treeSum = root.val + self.sumTree(root.left) + self.sumTree(root.right)
result = 0
def dfs(root):
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# O(h^2)
# Tree height = h
# Range of possible answers, r = 2^h
# First solution
class Solution:
def findLUSlength(self, a: str, b: str) -> int:
if len(a) != len(b):
return len(a) if len(a) > len(b) else len(b)
if a != b: return len(a)
return None
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# E.g.,
# [9, 15, 7, 20, 3] postorder
# [9, 3, 15, 20, 7] inorder
class Solution:
# Brute force. O(n!) I think, since it'll try all possible permutations
# TLE
class Solution:
def reorganizeString(self, S: str) -> str:
freqs = {}
for c in S:
if c not in freqs: freqs[c] = 1
else: freqs[c]+= 1
wc = []
// Memoized Recursive
// O(d*t)
// Unmemoized would be O((d*t)*f^d). I.e., recursive fn with branch factor f and max depth d
var numRollsToTarget = function(d, f, target) {
const memo = {};
const mod = Math.pow(10, 9) + 7;
const rtt = (d, t) => {
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {number} sum
/**
* @param {character[][]} board
* @return {boolean}
*/
var isValidSudoku = function(board) {
const allowedChars = new Set('123456789');
const rowSets = new Array(9).fill(0).map(()=> new Set());
const colSets = new Array(9).fill(0).map(()=> new Set());
/**
* @param {string} s
* @return {number}
*/
// Straight-up stack. O(n) time and space since every character
// is inspected at worst a fixed number of times.
var calculate = function(s) {
// question constraint: ignore unary negatives
const parsed = parse(s); // remove spaces, convert strings to ints
/**
* @param {string} s
* @return {number}
*/
// Straight-up stack. O(n) time and space since every character
// is inspected at worst a fixed number of times.
var calculate = function(s) {
// question constraint: ignore unary negatives
const parsed = parse(s); // remove spaces, convert strings to ints