921-Minimum-Add-To-Make-Parentheses-Valid
https://leetcode.com/problems/minimum-add-to-make-parentheses-valid
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minAddToMakeValid(self, s: str) -> int:
l = 0
r = 0
for c in s:
if c == '(':
l += 1
else:
if l == 0:
r += 1
else:
l -= 1
return l …
Read More
922-Sort-Array-By-Parity-Ii
https://leetcode.com/problems/sort-array-by-parity-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def sortArrayByParityII(self, A: List[int]) -> List[int]:
n = len(A)
i = 0
j = 1
while i < n:
while i < n and A[i] % 2 == 0:
i += 2
while …
Read More
925-Long-Pressed-Name
https://leetcode.com/problems/long-pressed-name
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isLongPressedName(self, name: str, typed: str) -> bool:
i = 0
for j, t in enumerate(typed):
if i < len(name) and name[i] == t:
i += 1
elif j == 0 …
Read More
926-Flip-String-To-Monotone-Increasing
https://leetcode.com/problems/flip-string-to-monotone-increasing
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minFlipsMonoIncr(self, S: str) -> int:
dp = [0] * 2
for i, c in enumerate(S):
dp[0], dp[1] = dp[0] + (c == '1'), min(dp[0], dp[1]) + (c …
Read More
927-Three-Equal-Parts
https://leetcode.com/problems/three-equal-parts
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def threeEqualParts(self, A: List[int]) -> List[int]:
ones = sum(a == 1 for a in A)
if ones == 0:
return [0, len(A) - 1]
if ones % 3 != 0:
return …
Read More
929-Unique-Email-Addresses
https://leetcode.com/problems/unique-email-addresses
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numUniqueEmails(self, emails: List[str]) -> int:
seen = set()
for email in emails:
local, domain = email.split('@')
local = local.split('+')[0].replace('.', '')
seen.add(local + '@' + domain)
return len(seen …
Read More
93-Restore-Ip-Addresses
https://leetcode.com/problems/restore-ip-addresses
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def restoreIpAddresses(self, s: str) -> List[str]:
ans = []
def dfs(start: int, path: List[int]) -> None:
if len(path) == 4 and start == len(s):
ans.append(path[0] + '.' + path …
Read More
930-Binary-Subarrays-With-Sum
https://leetcode.com/problems/binary-subarrays-with-sum
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numSubarraysWithSum(self, A: List[int], S: int) -> int:
ans = 0
prefix = 0
count = Counter({0: 1})
for a in A:
prefix += a
ans += count[prefix - S]
count[prefix …
Read More
931-Minimum-Falling-Path-Sum
https://leetcode.com/problems/minimum-falling-path-sum
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minFallingPathSum(self, A: List[List[int]]) -> int:
n = len(A)
for i in range(1, n):
for j in range(n):
mini = math.inf
for k in range …
Read More
932-Beautiful-Array
https://leetcode.com/problems/beautiful-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def beautifulArray(self, n: int) -> List[int]:
A = [i for i in range(1, n + 1)]
def partition(l: int, r: int, mask: int) -> int:
nextSwapped = l
for i …
Read More