1049-Last-Stone-Weight-Ii

Sat 17 May 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

Sat 17 May 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

Sat 17 May 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

Sat 17 May 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

Sat 17 May 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

Sat 17 May 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

1057-Campus-Bikes

Sat 17 May 2025

https://leetcode.com/problems/campus-bikes

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def assignBikes(self, workers: List[List[int]], bikes: List[List[int]]) -> List[int]:
    ans = [-1] * len(workers)
    usedBikes = [False] * len(bikes)
    # buckets[k] := (i, j), where k = dist(workers …

Category: leetcode

Read More

1058-Minimize-Rounding-Error-To-Meet-Target

Sat 17 May 2025

https://leetcode.com/problems/minimize-rounding-error-to-meet-target

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def minimizeError(self, prices: List[str], target: int) -> str:
    # A[i] := (costCeil - costFloor, costCeil, costFloor)
    # The lower the costCeil - costFloor, the cheaper to ceil it
    A = []
    sumFloored = 0
    sumCeiled …

Category: leetcode

Read More

106-Construct-Binary-Tree-From-Inorder-And-Postorder-Traversal

Sat 17 May 2025

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

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

    def build(inStart: int, inEnd: int, postStart: int, postEnd …

Category: leetcode

Read More

1063-Number-Of-Valid-Subarrays

Sat 17 May 2025

https://leetcode.com/problems/number-of-valid-subarrays

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def validSubarrays(self, nums: List[int]) -> int:
    # For each num in nums, each element x in the stack can be the leftmost
    # Element s.t. [x, num] forms a …

Category: leetcode

Read More
Page 5 of 77

« Prev Next »