525-Contiguous-Array
https://leetcode.com/problems/contiguous-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findMaxLength(self, nums: List[int]) -> int:
ans = 0
prefix = 0
prefixToIndex = {0: -1}
for i, num in enumerate(nums):
prefix += 1 if num else -1
if prefix in …
Read More
527-Word-Abbreviation
https://leetcode.com/problems/word-abbreviation
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def wordsAbbreviation(self, words: List[str]) -> List[str]:
n = len(words)
def getAbbrev(s: str, prefixIndex: int) -> str:
n = len(s)
num = n - (prefixIndex + 1) - 1
numLength = 1 if …
Read More
528-Random-Pick-With-Weight
https://leetcode.com/problems/random-pick-with-weight
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def __init__(self, w: List[int]):
self.prefix = list(itertools.accumulate(w))
def pickIndex(self) -> int:
target = randint(0, self.prefix[-1] - 1)
l = 0
r = len(self.prefix …
Read More
529-Minesweeper
https://leetcode.com/problems/minesweeper
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
if board[click[0]][click[1]] == 'M':
board[click[0]][click[1]] = 'X'
return board
dirs …
Read More
53-Maximum-Subarray
https://leetcode.com/problems/maximum-subarray
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
ans = -math.inf
summ = 0
for num in nums:
summ += num
ans = max(ans, summ)
summ = max(summ, 0)
return ans
Read More
531-Lonely-Pixel-I
https://leetcode.com/problems/lonely-pixel-i
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findLonelyPixel(self, picture: List[List[str]]) -> int:
m = len(picture)
n = len(picture[0])
ans = 0
rows = [0] * m # rows[i] := # Of Bs in rows i
cols = [0 …
Read More
532-K-Diff-Pairs-In-An-Array
https://leetcode.com/problems/k-diff-pairs-in-an-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
ans = 0
numToIndex = {num: i for i, num in enumerate(nums)}
for i, num in enumerate(nums):
target = num + k …
Read More
533-Lonely-Pixel-Ii
https://leetcode.com/problems/lonely-pixel-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findBlackPixel(self, picture: List[List[str]], target: int) -> int:
m = len(picture)
n = len(picture[0])
ans = 0
rows = [row.count('B') for row in picture]
cols = [col …
Read More
537-Complex-Number-Multiplication
https://leetcode.com/problems/complex-number-multiplication
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def complexNumberMultiply(self, a: str, b: str) -> str:
def getRealAndImag(s: str) -> tuple:
return int(s[:s.index('+')]), int(s[s.index('+') + 1:-1])
A, B = getRealAndImag(a)
C …
Read More
538-Convert-Bst-To-Greater-Tree
https://leetcode.com/problems/convert-bst-to-greater-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
prefix = 0
def reversedInorder(root: Optional[TreeNode]) -> None:
nonlocal prefix
if not root:
return
reversedInorder(root.right)
prefix += root.val
root …
Read More