class Solution: def toHex(self, x: int) -> str: """tc O(lgN) sc O(1) """ res = '' if x == 0: return '0' d = '0123456789abcdef' res = '' #for i in range(8): while x and len(res) < 8: #must add len(res) < 8 else it will loop forever when x...
[Read More]
127. Word Ladder
class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: """tc O(N* W*W) sc O(N*W*W) W: len(word) N: number of words in word list 1. preprocessing all string pattern with one character matches any char as hashmap {h*t: [hit,hot]} 2. start from biginWord do BFS, terminate untild find...
[Read More]
740. Delete and Earn
Refer to 198 class Solution: def deleteAndEarn(self, A: List[int]) -> int: """ tc O(N) sc O(N) N: max(A) dp[i] the most score can get for sorted number array [0,i] (both inclusive ) """ n = 10001 if n == 1: # optimization return A[0] x_sum = [0]*n for a in...
[Read More]
1889. Minimum Space Wasted From Packaging
class Solution: def minWastedSpace(self, packages: List[int], boxes: List[List[int]]) -> int: """ tc O(PlgP + all(BlgB)) P: package B: boxes from each supplier - sort packages and boxes - iterate through box in from each supplier, get the total box size that can iterate through end of packate - module it...
[Read More]
100. Same Tree
Recursion Solution # 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 isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: """ tc O(N) sc O(N) """ if not p and...
[Read More]