559-Maximum-Depth-Of-N-Ary-Tree
https://leetcode.com/problems/maximum-depth-of-n-ary-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxDepth(self, root: 'Node') -> int:
if not root:
return 0
if not root.children:
return 1
return 1 + max(self.maxDepth(child) for child in root.children)
Read More
56-Merge-Intervals
https://leetcode.com/problems/merge-intervals
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
ans = []
for interval in sorted(intervals):
if not ans or ans[-1][1] < interval[0]:
ans.append(interval)
else …
Read More
560-Subarray-Sum-Equals-K
https://leetcode.com/problems/subarray-sum-equals-k
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
ans = 0
prefix = 0
count = Counter({0: 1})
for num in nums:
prefix += num
ans += count[prefix - k]
count[prefix …
Read More
561-Array-Partition
https://leetcode.com/problems/array-partition
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
return sum(sorted(nums)[::2])
new Solution().arrayPairSum()
Score: 5
Read More
562-Longest-Line-Of-Consecutive-One-In-Matrix
https://leetcode.com/problems/longest-line-of-consecutive-one-in-matrix
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def longestLine(self, mat: List[List[int]]) -> int:
m = len(mat)
n = len(mat[0])
ans = 0
# dp[i][j][0] := horizontal
# dp[i][j][1] := vertical
# dp[i …
Read More
563-Binary-Tree-Tilt
https://leetcode.com/problems/binary-tree-tilt
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findTilt(self, root: Optional[TreeNode]) -> int:
ans = 0
def summ(root: Optional[TreeNode]) -> None:
nonlocal ans
if not root:
return 0
l = summ(root.left)
r = summ(root …
Read More
564-Find-The-Closest-Palindrome
https://leetcode.com/problems/find-the-closest-palindrome
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def nearestPalindromic(self, n: str) -> str:
def getPalindromes(s: str) -> tuple:
num = int(s)
k = len(s)
palindromes = []
half = s[0:(k + 1) // 2]
reversedHalf = half[:k // 2][::-1 …
Read More
565-Array-Nesting
https://leetcode.com/problems/array-nesting
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def arrayNesting(self, nums: List[int]) -> int:
ans = 0
for num in nums:
if num == -1:
continue
index = num
count = 0
while nums[index] != -1:
temp = index
index = nums …
Read More
566-Reshape-The-Matrix
https://leetcode.com/problems/reshape-the-matrix
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]:
if nums == [] or r * c != len(nums) * len(nums[0]):
return nums
ans = [[0 for …
Read More
57-Insert-Interval
https://leetcode.com/problems/insert-interval
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
n = len(intervals)
ans = []
i = 0
while i < n and intervals[i][1] < newInterval[0]:
ans …
Read More