```python class Solution: def wordBreak(self, s: str, wordDict: List[str]) -> bool: “”” main idea: Bottom up - put l, le, lee, leet into cache, and look up the dictionary to find if any word match dp[i] for substring s from idex 0 to index i-1, there is a word in...
[Read More]
LC19. Remove Nth Node From End of List
time O(N) space O(1) # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: # O(N) space O(1) dummy = ListNode() dummy.next = head # step1 cnt length leng...
[Read More]
LC19. Remove Nth Node From End of List
time O(N) space O(1)
```python
Definition for singly-linked list.
class ListNode:
def init(self, x):
self.val = x
self.next = None
[Read More]
LC543. Diameter of Binary Tree
# 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 diameterOfBinaryTree(self, root: TreeNode) -> int: self.max = 0 if not root: return 0 def dfs(node): if not node: return...
[Read More]
LC255. Verify Preorder Sequence in Binary Search Tree
class Solution: def verifyPreorder(self, preorder: List[int]) -> bool: """ Time O(N) space O(N) stack to track left substree and parent node note the stack is mono decreasing stack lo 2 [5,2,1] [5,3] """ st = [] lo = float('-inf') for p in preorder: print(p,lo,st) if p < lo: return False...
[Read More]