Skip to content

Commit 19c8d8d

Browse files
committed
Did some questions from the Uber most frequent and another hard Trie question
1 parent 5e928e2 commit 19c8d8d

File tree

9 files changed

+256
-10
lines changed

9 files changed

+256
-10
lines changed

.Readme Updater

README.md

+15-9
Large diffs are not rendered by default.

my-submissions/e94.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public List<Integer> inorderTraversal(TreeNode root) {
18+
ArrayList<Integer> output = new ArrayList<>();
19+
helper(root, output);
20+
return output;
21+
}
22+
23+
private void helper(TreeNode curr, ArrayList<Integer> output) {
24+
if (curr == null) {
25+
return;
26+
}
27+
helper(curr.left, output);
28+
output.add(curr.val);
29+
helper(curr.right, output);
30+
}
31+
}

my-submissions/e94.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
9+
self.output = []
10+
def helper(curr: Optional[TreeNode]) -> None :
11+
if not curr :
12+
return
13+
14+
helper(curr.left)
15+
self.output.append(curr.val)
16+
helper(curr.right)
17+
18+
helper(root)
19+
return self.output

my-submissions/h3045.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
class Solution:
2+
def countPrefixSuffixPairs(self, words: List[str]) -> int:
3+
# Bidirectional as in from both sides of the string
4+
# key = (x, y) --> x = ith char from front
5+
# --> y = ith char from end
6+
# Goes full length of string so the chars cross
7+
bidirectionalTrie = {}
8+
for i, word in enumerate(words) :
9+
curr = bidirectionalTrie
10+
for j in range(len(word)) :
11+
key = (word[j], word[len(word) - j - 1])
12+
if key not in curr :
13+
curr[key] = {}
14+
curr = curr[key]
15+
16+
if False not in curr :
17+
curr[False] = []
18+
curr[False].append(i)
19+
20+
21+
# Finding if the current word works as a prefix/suffix
22+
# of any word
23+
counter = 0
24+
for i, word in enumerate(words) :
25+
curr = bidirectionalTrie
26+
27+
for j in range(len(word)) :
28+
key = (word[j], word[len(word) - j - 1])
29+
if key not in curr :
30+
continue
31+
curr = curr[key]
32+
33+
counter += self.validWords(curr, i)
34+
35+
return counter
36+
37+
38+
def validWords(self, trie: dict, lowerBoundIndx: int) -> int :
39+
if not trie :
40+
return 0
41+
42+
# lowerBoundIndx + 1 cause we don't want to match a
43+
# word with itself. Plus, if it was equal, bisect_left/right
44+
# would give us the leftmost or rightmost of its own value if
45+
# it exists. We want the indx of the first greater than it
46+
# so +1 bisect left will make it the next bigger no matter what
47+
output = 0
48+
if False in trie :
49+
indxSplit = bisect_left(trie[False], lowerBoundIndx + 1)
50+
if indxSplit < len(trie[False]) :
51+
output += len(trie[False]) - indxSplit
52+
53+
for k, v in trie.items() :
54+
if k :
55+
output += self.validWords(v, lowerBoundIndx)
56+
57+
return output

my-submissions/m1268.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution:
2+
def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]:
3+
trie = {}
4+
5+
products.sort()
6+
7+
for product in products :
8+
curr = trie
9+
for c in product :
10+
if c not in curr :
11+
curr[c] = {}
12+
curr = curr[c]
13+
14+
curr[False] = True
15+
16+
output = []
17+
word = []
18+
for c in searchWord :
19+
temp = []
20+
if c not in trie :
21+
break
22+
trie = trie[c]
23+
word.append(c)
24+
self.counter = 3
25+
self.collectWordsFromPoint(trie, word, temp)
26+
output.append(temp)
27+
28+
while len(output) < len(searchWord) :
29+
output.append([])
30+
31+
32+
return output
33+
34+
def collectWordsFromPoint(self, trie: dict, current: List[str], output: List[str]) -> None :
35+
if not self.counter :
36+
return
37+
if not trie :
38+
return
39+
40+
if False in trie :
41+
output.append(''.join(current))
42+
self.counter -= 1
43+
44+
for letter, branch in trie.items() :
45+
if letter :
46+
current.append(letter)
47+
self.collectWordsFromPoint(branch, current, output)
48+
current.pop()

my-submissions/m230.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# In order traversal
2+
3+
# Definition for a binary tree node.
4+
# class TreeNode:
5+
# def __init__(self, val=0, left=None, right=None):
6+
# self.val = val
7+
# self.left = left
8+
# self.right = right
9+
class Solution:
10+
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
11+
self.output = -1
12+
self.counter = 0
13+
return self.helper(root, k)
14+
15+
def helper(self, curr: Optional[TreeNode], k: int) -> int :
16+
if not curr :
17+
return 0
18+
19+
temp = self.helper(curr.left, k)
20+
if temp :
21+
return temp
22+
23+
self.counter += 1
24+
if self.counter == k :
25+
return curr.val
26+
27+
return self.helper(curr.right, k)
28+

my-submissions/m2473.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution:
2+
def minCost(self, n: int, roads: List[List[int]], appleCost: List[int], k: int) -> List[int]:
3+
travelCostMultiplier = k + 1
4+
output = []
5+
6+
roadChoices = defaultdict(list)
7+
8+
for road in roads :
9+
roadChoices[road[0]].append((road[1], road[2]))
10+
roadChoices[road[1]].append((road[0], road[2]))
11+
12+
for i in range(1, n + 1) :
13+
toVisit = [] # (totalRoadCost, targetIndx)
14+
toVisit.append((0, i))
15+
distances = [inf] * n
16+
17+
while toVisit :
18+
cost, node = heapq.heappop(toVisit)
19+
if distances[node - 1] != inf :
20+
continue
21+
22+
distances[node - 1] = cost
23+
24+
for nextNode, additionalCost in roadChoices[node] :
25+
heapq.heappush(toVisit, (cost + additionalCost, nextNode))
26+
27+
output.append(min([distances[x] * travelCostMultiplier + appleCost[x] for x in range(n)]))
28+
29+
return output
30+

my-submissions/m384.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
3+
def __init__(self, nums: List[int]):
4+
self.nums = nums.copy()
5+
self.shuffled = nums.copy()
6+
7+
def reset(self) -> List[int]:
8+
self.shuffled = self.nums.copy()
9+
return self.nums
10+
11+
def shuffle(self) -> List[int]:
12+
indices = set(range(len(self.nums)))
13+
self.shuffled = []
14+
15+
while indices :
16+
temp = random.choice(list(indices))
17+
self.shuffled.append(self.nums[temp])
18+
indices.remove(temp)
19+
return self.shuffled
20+
21+
22+
23+
24+
# Your Solution object will be instantiated and called as such:
25+
# obj = Solution(nums)
26+
# param_1 = obj.reset()
27+
# param_2 = obj.shuffle()

0 commit comments

Comments
 (0)