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
# 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): | |
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
# 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 |
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
# 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 |
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
# 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: |
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
# 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 = [] |
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
// 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) => { | |
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
/** | |
* Definition for a binary tree node. | |
* function TreeNode(val) { | |
* this.val = val; | |
* this.left = this.right = null; | |
* } | |
*/ | |
/** | |
* @param {TreeNode} root | |
* @param {number} sum |
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
/** | |
* @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()); |
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
/** | |
* @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 |
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
/** | |
* @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 |