Skip to content

Commit a2b69cd

Browse files
committed
Daily + hard + a lottttt of question grinding from similar questions on BSTs and Islands
1 parent fc41736 commit a2b69cd

15 files changed

+595
-8
lines changed

README.md

Lines changed: 19 additions & 8 deletions
Large diffs are not rendered by default.

my-submissions/h2954.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# NOTE: Literally on the edge of runtimes lol
2+
# Passes runtime ~50% of the time, other half it barely fails
3+
4+
5+
''' Notes
6+
7+
if you have X _ _ _ X where X=sick and _=not sick, how many ways?
8+
9+
We can count the gaps then multiple the number of ways each gap can do it
10+
11+
1 person --> 1
12+
2 ppl --> 2
13+
3 ppl --> 4
14+
X__ XX_ XXX
15+
X__ X_X XXX
16+
__X _XX XXX
17+
__X X_X XXX
18+
19+
X____ XX__ XXX_ XXXX
20+
X____ XX__ XX_X XXXX
21+
X____ XX__ X_XX XXXX
22+
X____ X__X X_XX XXXX
23+
X____ X__X XX_X XXXX
24+
...
25+
26+
2*2*2*1?
27+
28+
Cause we have left and right as a choice each time till the last where they merge
29+
30+
Let n=size of gap
31+
32+
# Ways = 2^(n-1)
33+
34+
35+
The leftmost group and rightmost group only have 1 way since they're
36+
against a wall figuratively speaking
37+
38+
Edge cases (literally):
39+
X _ _ _ _ only 1 way
40+
_ X _ 2 ways
41+
_ _ X 1 way
42+
_ X _ _ 3 ways
43+
44+
_ _ X _ _ 6 ways
45+
Right-Left side is 4 choose 2?
46+
47+
m+n choose m? it's a binary string method
48+
49+
This also becomes a multimodal where we have all the groups and we
50+
have to figure out how to order our choices
51+
52+
53+
FINAL FORMULA:
54+
Product of:
55+
2^(g - 1) for each group surrounded by 2 sick individuals
56+
(L + R) choose (L) where L and R are the number of not sick people on the edges
57+
(n!)(a!b!c!...z!) where n=the total number of not sick indivudals
58+
and a,b,c...= the size of each not sick group
59+
'''
60+
61+
62+
class Solution:
63+
def numberOfSequence(self, n: int, sick: List[int]) -> int:
64+
minn, maxx = sick[0], sick[-1]
65+
66+
totalToInfect = 0
67+
groupSizes = [(0, 0)]
68+
output = 1
69+
70+
if minn > 0 :
71+
totalToInfect += minn
72+
groupSizes.append((minn, minn))
73+
if n - maxx - 1 > 0 :
74+
count = n - maxx - 1
75+
totalToInfect += count
76+
groupSizes.append((count, count + groupSizes[-1][1]))
77+
78+
for i in range(1, len(sick)) :
79+
numPeople = sick[i] - sick[i - 1] - 1
80+
if numPeople > 0 :
81+
totalToInfect += numPeople
82+
groupSizes.append((numPeople, numPeople + groupSizes[-1][1]))
83+
output *= 2 ** (numPeople - 1)
84+
85+
def multinomial(groups: List[Tuple[int, int]]):
86+
modd = 10 ** 9 + 7
87+
return math.prod(math.comb(x[1], x[0]) % (modd) for x in groups)
88+
89+
return (output * multinomial(groupSizes[1:])) % (10 ** 9 + 7)
90+
91+

my-submissions/m1020.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution:
2+
def numEnclaves(self, grid: List[List[int]]) -> int:
3+
def removeNonEnclave(row: int, col: int) -> None :
4+
if not (0 <= row < len(grid)) or not (0 <= col < len(grid[0])) :
5+
return
6+
if not grid[row][col] :
7+
return
8+
9+
grid[row][col] = 0
10+
11+
removeNonEnclave(row + 1, col)
12+
removeNonEnclave(row - 1, col)
13+
removeNonEnclave(row, col + 1)
14+
removeNonEnclave(row, col - 1)
15+
16+
for i in range(len(grid)) :
17+
if grid[i][0] :
18+
removeNonEnclave(i, 0)
19+
if grid[i][len(grid[0]) - 1] :
20+
removeNonEnclave(i, len(grid[0]) - 1)
21+
22+
for i in range(1, len(grid[0]) - 1) :
23+
if grid[0][i] :
24+
removeNonEnclave(0, i)
25+
if grid[len(grid) - 1][i] :
26+
removeNonEnclave(len(grid) - 1, i)
27+
28+
29+
counter = 0
30+
for row in range(1, len(grid)) :
31+
for col in range(len(grid[0])) :
32+
if grid[row][col] :
33+
counter += 1
34+
35+
return counter

my-submissions/m1038.c renamed to my-submissions/m1038 Daily.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Did this like 3 days before it was a daily question so I just had to resubmit for the daily lol
2+
13
/**
24
* Definition for a binary tree node.
35
* struct TreeNode {

my-submissions/m1254.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution:
2+
def closedIsland(self, grid: List[List[int]]) -> int:
3+
def deleteIsland(row: int, col: int) -> None :
4+
if not (0 <= row < len(grid)) or not (0 <= col < len(grid[0])) :
5+
return
6+
if grid[row][col] :
7+
return
8+
9+
grid[row][col] = 1
10+
11+
deleteIsland(row + 1, col)
12+
deleteIsland(row - 1, col)
13+
deleteIsland(row, col + 1)
14+
deleteIsland(row, col - 1)
15+
16+
for i in range(len(grid)) :
17+
if not grid[i][0] :
18+
deleteIsland(i, 0)
19+
if not grid[i][len(grid[0]) - 1] :
20+
deleteIsland(i, len(grid[0]) - 1)
21+
22+
for i in range(1, len(grid[0]) - 1) :
23+
if not grid[0][i] :
24+
deleteIsland(0, i)
25+
if not grid[len(grid) - 1][i] :
26+
deleteIsland(len(grid) - 1, i)
27+
28+
counter = 0
29+
30+
for row in range(len(grid)) :
31+
for col in range(len(grid[0])) :
32+
if not grid[row][col] :
33+
counter += 1
34+
deleteIsland(row, col)
35+
36+
return counter

my-submissions/m1382 AVL Method.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 balanceBST(self, root: TreeNode) -> TreeNode:
9+
# Calculate weights of each node
10+
weights = defaultdict(int)
11+
12+
def getWeights(curr: TreeNode) -> int :
13+
if not curr :
14+
return 0
15+
weight = getWeights(curr.left) + getWeights(curr.right) + 1
16+
weights[curr] = weight
17+
return weight
18+
19+
getWeights(root)
20+
21+
22+
# Helper methods to get the predecessor and ancestors
23+
def removeNextGreatest(curr: TreeNode) -> TreeNode :
24+
if curr.left and curr.left.left :
25+
weights[curr] -= 1
26+
return removeNextGreatest(curr.left)
27+
weights[curr] -= 1
28+
output = curr.left
29+
curr.left = output.right
30+
output.right = None
31+
return output
32+
33+
def removeNextSmaller(curr: TreeNode) -> TreeNode :
34+
if curr.right and curr.right.right :
35+
weights[curr] -= 1
36+
return removeNextSmaller(curr.right)
37+
weights[curr] -= 1
38+
output = curr.right
39+
curr.right = output.left
40+
output.left = None
41+
return output
42+
43+
44+
# Recursive method to balance each node
45+
def balanceBSTHelper(curr: TreeNode) -> TreeNode :
46+
if not curr :
47+
return None
48+
49+
if -1 <= weights[curr.left] - weights[curr.right] <= 1 :
50+
curr.left = balanceBSTHelper(curr.left)
51+
curr.right = balanceBSTHelper(curr.right)
52+
weights[curr] = 1 + weights[curr.left] + weights[curr.right]
53+
return curr
54+
55+
if weights[curr.right] - weights[curr.left] > 0 : # right heavier
56+
weights[curr] -= weights[curr.right]
57+
if not curr.right.left :
58+
newRoot = curr.right
59+
newRoot.left = curr
60+
curr.right = None
61+
weights[newRoot] += weights[newRoot.left]
62+
return balanceBSTHelper(newRoot)
63+
64+
newRoot = removeNextGreatest(curr.right)
65+
newRoot.right = curr.right
66+
newRoot.left = curr
67+
newRoot.left.right = None
68+
weights[newRoot] = weights[newRoot.left] + weights[newRoot.right] + 1
69+
70+
return balanceBSTHelper(newRoot)
71+
72+
# left heavier
73+
weights[curr] -= weights[curr.left]
74+
if not curr.left.right :
75+
newRoot = curr.left
76+
newRoot.right = curr
77+
curr.left = None
78+
weights[newRoot] += weights[newRoot.right]
79+
80+
return balanceBSTHelper(newRoot)
81+
82+
newRoot = removeNextSmaller(curr.left)
83+
newRoot.left = curr.left
84+
newRoot.right = curr
85+
newRoot.right.left = None
86+
weights[newRoot] = weights[newRoot.left] + weights[newRoot.right] + 1
87+
88+
return balanceBSTHelper(newRoot)
89+
90+
return balanceBSTHelper(root)

my-submissions/m142 v1 O(1) space.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
9+
if not head :
10+
return None
11+
12+
currFast = head
13+
currSlow = head
14+
15+
loop: bool = False
16+
while currFast.next and currFast.next.next :
17+
currFast = currFast.next.next
18+
currSlow = currSlow.next
19+
20+
if currFast == currSlow :
21+
loop = True
22+
break
23+
24+
if not loop :
25+
return None
26+
27+
loopSize = 1
28+
currFast = currFast.next
29+
while currFast != currSlow :
30+
loopSize += 1
31+
currFast = currFast.next
32+
33+
currRef = head
34+
while True :
35+
for i in range(loopSize) :
36+
currFast = currFast.next
37+
if currFast == currRef :
38+
return currRef
39+
currRef = currRef.next
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
9+
if not head :
10+
return None
11+
12+
past = set()
13+
14+
while head :
15+
if head in past :
16+
return head
17+
18+
past.add(head)
19+
head = head.next
20+
return None
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
8+
class Solution:
9+
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
10+
if not head :
11+
return None
12+
13+
currFast = head
14+
currSlow = head
15+
16+
while currFast.next and currFast.next.next :
17+
currFast = currFast.next.next
18+
currSlow = currSlow.next
19+
20+
if currFast == currSlow : # Loop found
21+
currSlow = head
22+
while currSlow != currFast :
23+
currSlow = currSlow.next
24+
currFast = currFast.next
25+
26+
return currSlow
27+
return None

my-submissions/m173.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 BSTIterator:
8+
def __init__(self, root: Optional[TreeNode]):
9+
self.val = root.val
10+
self.stk = []
11+
12+
while root :
13+
self.stk.append(root)
14+
root = root.left
15+
16+
def next(self) -> int:
17+
curr = self.stk.pop()
18+
output = curr.val
19+
20+
if curr.right :
21+
curr = curr.right
22+
while curr :
23+
self.stk.append(curr)
24+
curr = curr.left
25+
26+
return output
27+
28+
def hasNext(self) -> bool:
29+
return len(self.stk) > 0
30+
31+
32+
# Your BSTIterator object will be instantiated and called as such:
33+
# obj = BSTIterator(root)
34+
# param_1 = obj.next()
35+
# param_2 = obj.hasNext()

0 commit comments

Comments
 (0)