942-Di-String-Match

Sat 17 May 2025

https://leetcode.com/problems/di-string-match

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def diStringMatch(self, S: str) -> List[int]:
    ans = []
    mini = 0
    maxi = len(S)

    for c in S:
      if c == 'I':
        ans.append(mini)
        mini += 1
      else:
        ans.append(maxi …

Category: leetcode

Read More

945-Minimum-Increment-To-Make-Array-Unique

Sat 17 May 2025

https://leetcode.com/problems/minimum-increment-to-make-array-unique

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def minIncrementForUnique(self, A: List[int]) -> int:
    ans = 0
    minAvailable = 0

    A.sort()

    for a in A:
      ans += max(minAvailable - a, 0)
      minAvailable = max(minAvailable, a) + 1

    return ans …

Category: leetcode

Read More

946-Validate-Stack-Sequences

Sat 17 May 2025

https://leetcode.com/problems/validate-stack-sequences

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
    stack = []
    i = 0  # popped's index

    for x in pushed:
      stack.append(x)
      while stack and stack[-1] == popped …

Category: leetcode

Read More

948-Bag-Of-Tokens

Sat 17 May 2025

https://leetcode.com/problems/bag-of-tokens

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def bagOfTokensScore(self, tokens: List[int], power: int) -> int:
    ans = 0
    score = 0
    q = deque(sorted(tokens))

    while q and (power >= q[0] or score):
      while q and power …

Category: leetcode

Read More

949-Largest-Time-For-Given-Digits

Sat 17 May 2025

https://leetcode.com/problems/largest-time-for-given-digits

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def largestTimeFromDigits(self, A: List[int]) -> str:
    for time in itertools.permutations(sorted(A, reverse=True)):
      if time[:2] < (2, 4) and time[2] < 6:
        return '%d%d:%d …

Category: leetcode

Read More

95-Unique-Binary-Search-Trees-Ii

Sat 17 May 2025

https://leetcode.com/problems/unique-binary-search-trees-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def generateTrees(self, n: int) -> List[TreeNode]:
    if n == 0:
      return []

    def generateTrees(mini: int, maxi: int) -> List[Optional[int]]:
      if mini > maxi:
        return [None]

      ans = []

      for i in …

Category: leetcode

Read More

950-Reveal-Cards-In-Increasing-Order

Sat 17 May 2025

https://leetcode.com/problems/reveal-cards-in-increasing-order

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:
    q = deque()

    for card in reversed(sorted(deck)):
      q.rotate()
      q.appendleft(card)

    return list(q)
new Solution().deckRevealedIncreasing()

Score: 5 …

Category: leetcode

Read More

951-Flip-Equivalent-Binary-Trees

Sat 17 May 2025

https://leetcode.com/problems/flip-equivalent-binary-trees

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def flipEquiv(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool:
    if not root1:
      return not root2
    if not root2:
      return not root1
    if root1.val != root2.val:
      return False …

Category: leetcode

Read More

952-Largest-Component-Size-By-Common-Factor

Sat 17 May 2025

https://leetcode.com/problems/largest-component-size-by-common-factor

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class UnionFind:
  def __init__(self, n: int):
    self.id = [i for i in range(n + 1)]

  def union(self, u: int, v: int) -> bool:
    i = self.find(u)
    j = self.find …

Category: leetcode

Read More

953-Verifying-An-Alien-Dictionary

Sat 17 May 2025

https://leetcode.com/problems/verifying-an-alien-dictionary

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def isAlienSorted(self, words: List[str], order: str) -> bool:
    dict = {c: i for i, c in enumerate(order)}
    words = [[dict[c] for c in word] for word in words …

Category: leetcode

Read More
Page 73 of 77

« Prev Next »