907-Sum-Of-Subarray-Minimums
https://leetcode.com/problems/sum-of-subarray-minimums
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
kMod = 1_000_000_007
n = len(arr)
ans = 0
# prev[i] := index k s.t. arr[k] is the prev min in arr …
Read More
908-Smallest-Range-I
https://leetcode.com/problems/smallest-range-i
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def smallestRangeI(self, A: List[int], K: int) -> int:
return max(0, max(A) - min(A) - 2 * K)
new Solution().smallestRangeI()
Score: 5
Read More
909-Snakes-And-Ladders
https://leetcode.com/problems/snakes-and-ladders
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def snakesAndLadders(self, board: List[List[int]]) -> int:
n = len(board)
ans = 0
q = deque([1])
seen = set()
A = [0] * (1 + n * n) # 2D -> 1D
for i …
Read More
91-Decode-Ways
https://leetcode.com/problems/decode-ways
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numDecodings(self, s: str) -> int:
n = len(s)
# dp[i] := # Of ways to decode s[i..n)
dp = [0] * n + [1]
def isValid(a: chr, b=None) -> bool …
Read More
910-Smallest-Range-Ii
https://leetcode.com/problems/smallest-range-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def smallestRangeII(self, A: List[int], K: int) -> int:
A.sort()
ans = A[-1] - A[0]
left = A[0] + K
right = A[-1] - K
for a, b in zip …
Read More
912-Sort-An-Array
https://leetcode.com/problems/sort-an-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def mergeSort(A: List[int], l: int, r: int) -> None:
if l >= r:
return
def merge(A: List[int], l: int …
Read More
913-Cat-And-Mouse
https://leetcode.com/problems/cat-and-mouse
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from enum import IntEnum
class State(IntEnum):
kDraw = 0
kMouseWin = 1
kCatWin = 2
class Solution:
def catMouseGame(self, graph: List[List[int]]) -> int:
n = len(graph)
# Result of (cat, mouse, move …
Read More
914-X-Of-A-Kind-In-A-Deck-Of-Cards
https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def hasGroupsSizeX(self, deck: List[int]) -> bool:
count = Counter(deck)
return functools.reduce(math.gcd, count.values()) >= 2
new Solution().hasGroupsSizeX()
Score: 5
Read More
915-Partition-Array-Into-Disjoint-Intervals
https://leetcode.com/problems/partition-array-into-disjoint-intervals
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def partitionDisjoint(self, A: List[int]) -> int:
n = len(A)
mini = [0] * (n - 1) + [A[-1]]
maxi = -math.inf
for i in range(n - 2, - 1, -1):
mini[i …
Read More
916-Word-Subsets
https://leetcode.com/problems/word-subsets
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def wordSubsets(self, A: List[str], B: List[str]) -> List[str]:
count = Counter()
for b in B:
count = count | Counter(b)
return [a for a in A if Counter …
Read More