921-Minimum-Add-To-Make-Parentheses-Valid

Fri 14 November 2025

https://leetcode.com/problems/minimum-add-to-make-parentheses-valid

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

922-Sort-Array-By-Parity-Ii

Fri 14 November 2025

https://leetcode.com/problems/sort-array-by-parity-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

925-Long-Pressed-Name

Fri 14 November 2025

https://leetcode.com/problems/long-pressed-name

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

926-Flip-String-To-Monotone-Increasing

Fri 14 November 2025

https://leetcode.com/problems/flip-string-to-monotone-increasing

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

927-Three-Equal-Parts

Fri 14 November 2025

https://leetcode.com/problems/three-equal-parts

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

929-Unique-Email-Addresses

Fri 14 November 2025

https://leetcode.com/problems/unique-email-addresses

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

93-Restore-Ip-Addresses

Fri 14 November 2025

https://leetcode.com/problems/restore-ip-addresses

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

930-Binary-Subarrays-With-Sum

Fri 14 November 2025

https://leetcode.com/problems/binary-subarrays-with-sum

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

931-Minimum-Falling-Path-Sum

Fri 14 November 2025

https://leetcode.com/problems/minimum-falling-path-sum

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

932-Beautiful-Array

Fri 14 November 2025

https://leetcode.com/problems/beautiful-array

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More
Page 78 of 146

« Prev Next »