834-Sum-Of-Distances-In-Tree
https://leetcode.com/problems/sum-of-distances-in-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def sumOfDistancesInTree(self, N: int, edges: List[List[int]]) -> List[int]:
ans = [0] * N
count = [1] * N
tree = defaultdict(set)
for u, v in edges:
tree[u].add(v …
Read More
835-Image-Overlap
https://leetcode.com/problems/image-overlap
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def largestOverlap(self, A: List[List[int]], B: List[List[int]]) -> int:
n = len(A)
magic = 100
onesA = []
onesB = []
dict = defaultdict(int)
for i in range(n):
for j …
Read More
836-Rectangle-Overlap
https://leetcode.com/problems/rectangle-overlap
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isRectangleOverlap(self, rec1: List[int], rec2: List[int]) -> bool:
return rec1[0] < rec2[2] and rec2[0] < rec1[2] and rec1[1] < rec2[3] and rec2[1] < rec1 …
Read More
837-New-21-Game
https://leetcode.com/problems/new-21-game
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def new21Game(self, n: int, k: int, maxPts: int) -> float:
# When the game ends, the point is in [k..k - 1 + maxPts]
# P = 1, if n >= k - 1 + maxPts …
Read More
838-Push-Dominoes
https://leetcode.com/problems/push-dominoes
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def pushDominoes(self, dominoes: str) -> str:
ans = list(dominoes)
L = -1
R = -1
for i in range(len(dominoes) + 1):
if i == len(dominoes) or dominoes[i] == 'R':
if …
Read More
84-Largest-Rectangle-In-Histogram
https://leetcode.com/problems/largest-rectangle-in-histogram
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
ans = 0
stack = []
for i in range(len(heights) + 1):
while stack and (i == len(heights) or heights[stack[-1]] > heights[i …
Read More
840-Magic-Squares-In-Grid
https://leetcode.com/problems/magic-squares-in-grid
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numMagicSquaresInside(self, grid: List[List[int]]) -> int:
def isMagic(i: int, j: int) -> int:
s = "".join(str(grid[i + num // 3][j + num % 3])
for num in [0 …
Read More
841-Keys-And-Rooms
https://leetcode.com/problems/keys-and-rooms
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
seen = [False] * len(rooms)
def dfs(node: int) -> None:
seen[node] = True
for child in rooms[node]:
if not seen[child …
Read More
843-Guess-The-Word
https://leetcode.com/problems/guess-the-word
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
# """
# This is Master's API interface.
# You should not implement it, or speculate about its implementation
# """
# Class Master:
# def guess(self, word: str) -> int:
class Solution:
def findSecretWord(self, wordlist: List …
Read More
845-Longest-Mountain-In-Array
https://leetcode.com/problems/longest-mountain-in-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def longestMountain(self, A: List[int]) -> int:
ans = 0
i = 0
while i + 1 < len(A):
while i + 1 < len(A) and A[i] == A[i + 1]:
i += 1 …
Read More