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
917-Reverse-Only-Letters
https://leetcode.com/problems/reverse-only-letters
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def reverseOnlyLetters(self, S: str) -> str:
S = list(S)
i = 0
j = len(S) - 1
while i < j:
while i < j and not S[i].isalpha():
i += 1
while …
Read More
918-Maximum-Sum-Circular-Subarray
https://leetcode.com/problems/maximum-sum-circular-subarray
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxSubarraySumCircular(self, A: List[int]) -> int:
totalSum = 0
currMaxSum = 0
currMinSum = 0
maxSum = -math.inf
minSum = math.inf
for a in A:
totalSum += a
currMaxSum = max(currMaxSum + a …
Read More
92-Reverse-Linked-List-Ii
https://leetcode.com/problems/reverse-linked-list-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
if left == 1:
return self.reverseN(head, right)
head.next = self.reverseBetween(head.next, left - 1, right …
Read More