41-First-Missing-Positive
https://leetcode.com/problems/first-missing-positive
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
n = len(nums)
# Correct slot:
# nums[i] = i + 1
# nums[i] - 1 = i
# nums[nums[i] - 1] = nums[i]
for i in …
Read More
410-Split-Array-Largest-Sum
https://leetcode.com/problems/split-array-largest-sum
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def splitArray(self, nums: List[int], m: int) -> int:
n = len(nums)
prefix = [0] + list(itertools.accumulate(nums))
# Dp(i, k) := min of largest sum to split first i …
Read More
411-Minimum-Unique-Word-Abbreviation
https://leetcode.com/problems/minimum-unique-word-abbreviation
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minAbbreviation(self, target: str, dictionary: List[str]) -> str:
m = len(target)
def getMask(word: str) -> int:
# mask[i] = 0 := target[i] == word[i]
# mask[i] = 1 := target[i …
Read More
412-Fizz-Buzz
https://leetcode.com/problems/fizz-buzz
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def fizzBuzz(self, n: int) -> List[str]:
d = {3: 'Fizz', 5: 'Buzz'}
return [''.join([d[k] for k in d if i % k == 0]) or str(i) for i …
Read More
415-Add-Strings
https://leetcode.com/problems/add-strings
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def addStrings(self, num1: str, num2: str) -> str:
ans = []
carry = 0
i = len(num1) - 1
j = len(num2) - 1
while i >= 0 or j >= 0 or carry:
if i …
Read More
416-Partition-Equal-Subset-Sum
https://leetcode.com/problems/partition-equal-subset-sum
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def canPartition(self, nums: List[int]) -> bool:
summ = sum(nums)
if summ & 1:
return False
return self.knapsack_(nums, summ // 2)
def knapsack_(self, nums: List[int], subsetSum: int …
Read More
417-Pacific-Atlantic-Water-Flow
https://leetcode.com/problems/pacific-atlantic-water-flow
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
m = len(heights)
n = len(heights[0])
dirs = [0, 1, 0, -1, 0]
qP = deque()
qA = deque()
seenP = [[False …
Read More
418-Sentence-Screen-Fitting
https://leetcode.com/problems/sentence-screen-fitting
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def wordsTyping(self, sentence: List[str], rows: int, cols: int) -> int:
combined = ' '.join(sentence) + ' '
n = len(combined)
i = 0
for _ in range(rows):
i += cols
if combined[i …
Read More
42-Trapping-Rain-Water
https://leetcode.com/problems/trapping-rain-water
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def trap(self, height: List[int]) -> int:
n = len(height)
l = [0] * n # l[i] := max(height[0..i])
r = [0] * n # r[i] := max(height[i..n))
for …
Read More
423-Reconstruct-Original-Digits-From-English
https://leetcode.com/problems/reconstruct-original-digits-from-english
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def originalDigits(self, s: str) -> str:
count = [0] * 10
for c in s:
if c == 'z':
count[0] += 1
if c == 'o':
count[1] += 1
if c == 'w':
count …
Read More