522-Longest-Uncommon-Subsequence-Ii
https://leetcode.com/problems/longest-uncommon-subsequence-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findLUSlength(self, strs: List[str]) -> int:
def isSubsequence(a: str, b: str) -> bool:
i = 0
j = 0
while i < len(a) and j < len(b):
if a[i …
Read More
523-Continuous-Subarray-Sum
https://leetcode.com/problems/continuous-subarray-sum
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def checkSubarraySum(self, nums: List[int], k: int) -> bool:
prefix = 0
prefixToIndex = {0: -1}
for i, num in enumerate(nums):
prefix += num
if k != 0:
prefix %= k
if prefix …
Read More
524-Longest-Word-In-Dictionary-Through-Deleting
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findLongestWord(self, s: str, d: List[str]) -> str:
ans = ''
for word in d:
i = 0
for c in s:
if i < len(word) and c == word[i]:
i …
Read More
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