61-Rotate-List

Fri 14 November 2025

https://leetcode.com/problems/rotate-list

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def rotateRight(self, head: ListNode, k: int) -> ListNode:
    if not head or not head.next or k == 0:
      return head

    tail = head
    length = 1
    while tail.next:
      tail = tail …

Category: leetcode

Read More

611-Valid-Triangle-Number

Fri 14 November 2025

https://leetcode.com/problems/valid-triangle-number

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

    nums.sort()

    for k in range(len(nums) - 1, 1, -1):
      i = 0
      j = k - 1
      while i < j:
        if …

Category: leetcode

Read More

616-Add-Bold-Tag-In-String

Fri 14 November 2025

https://leetcode.com/problems/add-bold-tag-in-string

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def addBoldTag(self, s: str, words: List[str]) -> str:
    n = len(s)
    ans = []
    # bold[i] := True if s[i] should be bolded
    bold = [0] * n

    boldEnd = -1  # s[i …

Category: leetcode

Read More

617-Merge-Two-Binary-Trees

Fri 14 November 2025

https://leetcode.com/problems/merge-two-binary-trees

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
    if not root1 and not root2:
      return None
    val = (root1.val if root1 else 0) + (root2.val if …

Category: leetcode

Read More

62-Unique-Paths

Fri 14 November 2025

https://leetcode.com/problems/unique-paths

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def uniquePaths(self, m: int, n: int) -> int:
    # dp[i][j] := unique paths from (0, 0) to (i, j)
    dp = [[1] * n for _ in range(m)]

    for i …

Category: leetcode

Read More

621-Task-Scheduler

Fri 14 November 2025

https://leetcode.com/problems/task-scheduler

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def leastInterval(self, tasks: List[str], n: int) -> int:
    count = Counter(tasks)
    maxFreq = max(count.values())
    # Put the most frequent task in the slot first
    maxFreqTaskOccupy = (maxFreq - 1) * (n …

Category: leetcode

Read More

624-Maximum-Distance-In-Arrays

Fri 14 November 2025

https://leetcode.com/problems/maximum-distance-in-arrays

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

    for A in arrays:
      ans = max(ans, A[-1] - mini, maxi - A[0])
      mini = min …

Category: leetcode

Read More

625-Minimum-Factorization

Fri 14 November 2025

https://leetcode.com/problems/minimum-factorization

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def smallestFactorization(self, num: int) -> int:
    if num == 1:
      return 1

    ans = 0
    base = 1

    for i in range(9, 1, -1):
      while num % i == 0:
        num //= i
        ans …

Category: leetcode

Read More

628-Maximum-Product-Of-Three-Numbers

Fri 14 November 2025

https://leetcode.com/problems/maximum-product-of-three-numbers

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def maximumProduct(self, nums: List[int]) -> int:
    nums.sort()
    return max(nums[-1] * nums[0] * nums[1],
               nums[-1] * nums[-2] * nums[-3])
new Solution().maximumProduct()

Score: 5

Category: leetcode

Read More

629-K-Inverse-Pairs-Array

Fri 14 November 2025

https://leetcode.com/problems/k-inverse-pairs-array

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def kInversePairs(self, n: int, k: int) -> int:
    kMod = 1_000_000_007
    # dp[i][j] := # Of permutations of numbers 1..i with j inverse pairs
    dp = [[0] * (k + 1) for …

Category: leetcode

Read More
Page 53 of 146

« Prev Next »