Skip to content

Commit 5e928e2

Browse files
committed
Completed Daily + Weekly Premium + Similar Questions
1 parent 320177e commit 5e928e2

7 files changed

+255
-100
lines changed

README.md

+103-100
Large diffs are not rendered by default.

my-submissions/m2192 Daily.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# We can basically do levelorder traversals starting at 0, 1, 2,...
2+
# with compensating recursive calls
3+
4+
class Solution:
5+
def getAncestors(self, n: int, edges: List[List[int]]) -> List[List[int]]:
6+
directAncestors = [set() for _ in range(n)]
7+
visited = [False] * n
8+
9+
# Add immediate predecessors
10+
for source, target in edges :
11+
directAncestors[target].add(source)
12+
13+
14+
# Helper method that merges a node's ancestor set with
15+
# any ancestor's ancestor set
16+
def helper(currentIndx: int) -> None :
17+
if visited[currentIndx] :
18+
return
19+
20+
visited[currentIndx] = True
21+
if not directAncestors[currentIndx] : # is oldest ancestor
22+
return
23+
24+
mergeSet = set()
25+
for item in directAncestors[currentIndx] :
26+
helper(item) # make sure it's been initialized first
27+
mergeSet |= directAncestors[item]
28+
29+
directAncestors[currentIndx] |= mergeSet
30+
31+
for i in range(n) :
32+
if not visited[i] :
33+
helper(i)
34+
35+
36+
return [sorted(list(x)) for x in directAncestors]

my-submissions/m2192.java

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Solution {
2+
public List<List<Integer>> getAncestors(int n, int[][] edges) {
3+
ArrayList<HashSet<Integer>> parents = new ArrayList<>();
4+
5+
for (int i = 0; i < n; i++) {
6+
parents.add(new HashSet<Integer>());
7+
}
8+
9+
boolean[] visited = new boolean[n];
10+
11+
for (int[] edge : edges) {
12+
parents.get(edge[1]).add(edge[0]);
13+
}
14+
15+
for (int i = 0; i < n; i++) {
16+
if (!visited[i]) {
17+
helper(i, visited, parents);
18+
}
19+
}
20+
21+
List<List<Integer>> output = new ArrayList<>();
22+
for (int i = 0; i < n; i++) {
23+
output.add(new ArrayList<Integer>());
24+
output.get(i).addAll(parents.get(i));
25+
Collections.sort(output.get(i));
26+
}
27+
28+
return output;
29+
30+
}
31+
32+
private void helper(int currentIndx, boolean[] visited, ArrayList<HashSet<Integer>> parents) {
33+
if (visited[currentIndx]) {
34+
return;
35+
}
36+
37+
visited[currentIndx] = true;
38+
if (parents.get(currentIndx).size() == 0) {
39+
return;
40+
}
41+
42+
HashSet<Integer> mergeValues = new HashSet<>();
43+
for (Integer i : parents.get(currentIndx)) {
44+
helper(i, visited, parents);
45+
mergeValues.addAll(parents.get(i));
46+
}
47+
parents.get(currentIndx).addAll(mergeValues);
48+
}
49+
50+
}

my-submissions/m450.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
9+
def helper(curr: Optional[TreeNode], key: int) -> Optional[TreeNode] :
10+
if not curr :
11+
return None
12+
if curr.val == key :
13+
if not curr.left :
14+
return curr.right
15+
if not curr.left.right :
16+
curr.left.right = curr.right
17+
return curr.left
18+
19+
predecessor = curr.left
20+
21+
while predecessor.right and predecessor.right.right :
22+
predecessor = predecessor.right
23+
24+
curr.val = predecessor.right.val
25+
predecessor.right = predecessor.right.left
26+
27+
return curr
28+
29+
if curr.val < key :
30+
curr.right = helper(curr.right, key)
31+
return curr
32+
33+
curr.left = helper(curr.left, key)
34+
return curr
35+
36+
return helper(root, key)

my-submissions/m776 Weekly Premium.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# If greater than root, root is part of output (indx 0) and we need to find first
2+
# value greater than that for other node
3+
4+
# If less than root, root is part of output (indx 1) and we have to find
5+
# smaller values go left
6+
7+
# Definition for a binary tree node.
8+
# class TreeNode:
9+
# def __init__(self, val=0, left=None, right=None):
10+
# self.val = val
11+
# self.left = left
12+
# self.right = right
13+
class Solution:
14+
def splitBST(self, root: Optional[TreeNode], target: int) -> List[Optional[TreeNode]]:
15+
if not root :
16+
return [None, None]
17+
18+
if root.val == target :
19+
temp = root.right
20+
root.right = None
21+
return [root, temp]
22+
23+
if root.val > target :
24+
temp = self.splitBST(root.left, target)
25+
root.left = temp[1]
26+
return [temp[0], root]
27+
28+
temp = self.splitBST(root.right, target)
29+
root.right = temp[0]
30+
return [root, temp[1]]

0 commit comments

Comments
 (0)