File tree Expand file tree Collapse file tree 5 files changed +122
-0
lines changed
construct-binary-tree-from-preorder-and-inorder-traversal
implement-trie-prefix-tree
kth-smallest-element-in-a-bst Expand file tree Collapse file tree 5 files changed +122
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ def combinationSum (self , candidates : List [int ], target : int ) -> List [List [int ]]:
3+ dp = [[] for i in range (target + 1 )]
4+ dp [0 ] = [[]]
5+
6+ for num in candidates :
7+ for i in range (num , target + 1 ):
8+ for comb in dp [i - num ]:
9+ dp [i ].append (comb + [num ])
10+
11+ return dp [target ]
12+
13+ ## TC: O(outer loop * 1st inner(target size) * 2nd inner(combination #))
14+ ## SC: O(target size * combination #)
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def buildTree (self , preorder : List [int ], inorder : List [int ]) -> Optional [TreeNode ]:
3+ if len (inorder ) == 0 :
4+ return None
5+
6+ if len (preorder ) == 1 :
7+ return TreeNode (preorder [0 ])
8+
9+ idx = inorder .index (preorder .pop (0 ))
10+ node = TreeNode (inorder [idx ])
11+
12+ node .left = self .buildTree (preorder , inorder [:idx ])
13+ node .right = self .buildTree (preorder , inorder [idx + 1 :])
14+
15+ return node
16+
17+
18+ ## TC: O(n^2), SC: O(n)
Original file line number Diff line number Diff line change 1+ class TrieNode :
2+ def __init__ (self ):
3+ self .children = {}
4+ self .isEnd = False
5+
6+
7+ class Trie :
8+ def __init__ (self ):
9+ self .root = TrieNode ()
10+
11+ def insert (self , word : str ) -> None :
12+ cur = self .root
13+
14+ for c in word :
15+ if c not in cur .children :
16+ cur .children [c ] = TrieNode ()
17+ cur = cur .children [c ]
18+ cur .isEnd = True
19+
20+ ## TC: O(len(word)), SC: O(len(word))
21+
22+ def search (self , word : str ) -> bool :
23+ cur = self .root
24+
25+ for c in word :
26+ if c not in cur .children :
27+ return False
28+ cur = cur .children [c ]
29+ return cur .isEnd
30+
31+ ## TC: O(len(word)), SC: O(1)
32+
33+ def startsWith (self , prefix : str ) -> bool :
34+ cur = self .root
35+
36+ for c in prefix :
37+ if c not in cur .children :
38+ return False
39+ cur = cur .children [c ]
40+ return True
41+
42+ ## TC: O(len(prefix)). SC: O(1)
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def kthSmallest (self , root : Optional [TreeNode ], k : int ) -> int :
3+ ans = []
4+
5+ def helper (node ):
6+ if not node : return
7+ helper (node .left )
8+
9+ if len (ans ) == k :
10+ return
11+
12+ ans .append (node .val )
13+ helper (node .right )
14+
15+ helper (root )
16+ return ans [- 1 ]
17+
18+ ## TC: O(n), SC: O(h+n) where h == heights(tree) and n == element(tree)
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def exist (self , board : List [List [str ]], word : str ) -> bool :
3+ rows , cols = len (board ), len (board [0 ])
4+
5+ def backtrack (r , c , index ):
6+ if index == len (word ):
7+ return True
8+ if r < 0 or r >= rows or c < 0 or c >= cols or board [r ][c ] != word [index ]:
9+ return False
10+
11+ temp = board [r ][c ]
12+ board [r ][c ] = "!"
13+
14+ found = (backtrack (r + 1 , c , index + 1 ) or
15+ backtrack (r - 1 , c , index + 1 ) or
16+ backtrack (r , c + 1 , index + 1 ) or
17+ backtrack (r , c - 1 , index + 1 ))
18+
19+ board [r ][c ] = temp
20+ return found
21+
22+ for i in range (rows ):
23+ for j in range (cols ):
24+ if backtrack (i , j , 0 ):
25+ return True
26+
27+ return False
28+
29+ ## TC: O(D of row * D of Cols * 4^L), but amortised could be O(DoR * DoC)
30+ ## SC: O(L)
You can’t perform that action at this time.
0 commit comments