1041-Robot-Bounded-In-Circle

Fri 14 November 2025

https://leetcode.com/problems/robot-bounded-in-circle

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def isRobotBounded(self, instructions: str) -> bool:
    x = 0
    y = 0
    d = 0
    directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]

    for instruction in instructions:
      if instruction == 'G':
        x …

Category: leetcode

Read More

1042-Flower-Planting-With-No-Adjacent

Fri 14 November 2025

https://leetcode.com/problems/flower-planting-with-no-adjacent

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def gardenNoAdj(self, n: int, paths: List[List[int]]) -> List[int]:
    ans = [0] * n  # ans[i] := 1, 2, 3, or 4
    graph = [[] for _ in range(n)]

    for a …

Category: leetcode

Read More

1044-Longest-Duplicate-Substring

Fri 14 November 2025

https://leetcode.com/problems/longest-duplicate-substring

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def longestDupSubstring(self, s: str) -> str:
    kMod = 1_000_000_007
    bestStart = -1
    l = 1
    r = len(s)

    def val(c: str) -> int:
      return ord(c) - ord('a')

    # K := length of …

Category: leetcode

Read More

1048-Longest-String-Chain

Fri 14 November 2025

https://leetcode.com/problems/longest-string-chain

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def longestStrChain(self, words: List[str]) -> int:
    wordsSet = set(words)

    # Dp(s) := longest chain where s is the last word
    @functools.lru_cache(None)
    def dp(s: str) -> int:
      ans …

Category: leetcode

Read More

1049-Last-Stone-Weight-Ii

Fri 14 November 2025

https://leetcode.com/problems/last-stone-weight-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def lastStoneWeightII(self, stones: List[int]) -> int:
    summ = sum(stones)
    s = 0
    dp = [True] + [False] * summ

    for stone in stones:
      for w in range(summ // 2 + 1)[::-1]:
        if …

Category: leetcode

Read More

105-Construct-Binary-Tree-From-Preorder-And-Inorder-Traversal

Fri 14 November 2025

https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
    inToIndex = {num: i for i, num in enumerate(inorder)}

    def build(preStart: int, preEnd: int, inStart: int, inEnd …

Category: leetcode

Read More

1051-Height-Checker

Fri 14 November 2025

https://leetcode.com/problems/height-checker

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def heightChecker(self, heights: List[int]) -> int:
    ans = 0
    currentHeight = 1
    count = [0] * 101

    for height in heights:
      count[height] += 1

    for height in heights:
      while count[currentHeight] == 0 …

Category: leetcode

Read More

1052-Grumpy-Bookstore-Owner

Fri 14 November 2025

https://leetcode.com/problems/grumpy-bookstore-owner

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def maxSatisfied(self, customers: List[int], grumpy: List[int], X: int) -> int:
    satisfied = sum(c for i, c in enumerate(customers) if grumpy[i] == 0)
    madeSatisfied = 0
    windowSatisfied = 0 …

Category: leetcode

Read More

1053-Previous-Permutation-With-One-Swap

Fri 14 November 2025

https://leetcode.com/problems/previous-permutation-with-one-swap

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def prevPermOpt1(self, A: List[int]) -> List[int]:
    n = len(A)
    l = n - 2
    r = n - 1

    while l >= 0 and A[l] <= A[l + 1]:
      l -= 1
    if …

Category: leetcode

Read More

1054-Distant-Barcodes

Fri 14 November 2025

https://leetcode.com/problems/distant-barcodes

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
    ans = [0] * len(barcodes)
    count = Counter(barcodes)
    i = 0  # ans' index
    maxNum = max(count, key=count.get)

    def fillAns(num: int …

Category: leetcode

Read More
Page 6 of 146

« Prev Next »