365-Water-And-Jug-Problem

Fri 14 November 2025

https://leetcode.com/problems/water-and-jug-problem

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def canMeasureWater(self, jug1Capacity: int, jug2Capacity: int, targetCapacity: int) -> bool:
    return targetCapacity == 0 or \
        jug1Capacity + jug2Capacity >= targetCapacity and \
        targetCapacity % gcd(jug1Capacity, jug2Capacity) == 0
new Solution().canMeasureWater()

Score: 5

Category: leetcode

Read More

367-Valid-Perfect-Square

Fri 14 November 2025

https://leetcode.com/problems/valid-perfect-square

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

    while l < r:
      m = (l + r) // 2
      if m >= num / m:
        r = m
      else:
        l = m + 1

    return l …

Category: leetcode

Read More

368-Largest-Divisible-Subset

Fri 14 November 2025

https://leetcode.com/problems/largest-divisible-subset

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def largestDivisibleSubset(self, nums: List[int]) -> List[int]:
    n = len(nums)
    ans = []
    count = [1] * n
    prevIndex = [-1] * n
    maxCount = 0
    index = -1

    nums.sort()

    for i, num in enumerate …

Category: leetcode

Read More

37-Relative-Vigor-Index

Fri 14 November 2025
# Created: 20250104
import pyutil as pyu
pyu.get_local_pyinfo()
'conda env: ml312-2024; pyv: 3.12.7 | packaged by Anaconda, Inc. | (main, Oct  4 2024, 13:27:36) [GCC 11.2.0]'
print(pyu.ps2("yfinance pandas matplotlib"))
yfinance==0.2.51
pandas==2.2.3
matplotlib==3.9.3
import …

Category: stockmarket

Read More

37-Sudoku-Solver

Fri 14 November 2025

https://leetcode.com/problems/sudoku-solver

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def solveSudoku(self, board: List[List[str]]) -> None:
    def isValid(row: int, col: int, c: chr) -> bool:
      for i in range(9):
        if board[i][col] == c or \
           board …

Category: leetcode

Read More

371-Sum-Of-Two-Integers

Fri 14 November 2025

https://leetcode.com/problems/sum-of-two-integers

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def getSum(self, a: int, b: int) -> int:
    mask = 0xFFFFFFFF
    kMax = 2000

    while b:
      a, b = (a ^ b) & mask, ((a & b) << 1) & mask

    return a if a < kMax else …

Category: leetcode

Read More

372-Super-Pow

Fri 14 November 2025

https://leetcode.com/problems/super-pow

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def superPow(self, a: int, b: List[int]) -> int:
    def powMod(x: int, y: int) -> int:
      pow = 1
      for _ in range(y):
        pow = (pow * x) % k
      return pow …

Category: leetcode

Read More

375-Guess-Number-Higher-Or-Lower-Ii

Fri 14 November 2025

https://leetcode.com/problems/guess-number-higher-or-lower-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def getMoneyAmount(self, n: int) -> int:
    # Dp(i, j) := min money you need to guarantee a win of picking i..j
    @functools.lru_cache(None)
    def dp(i: int, j …

Category: leetcode

Read More

377-Combination-Sum-Iv

Fri 14 November 2025

https://leetcode.com/problems/combination-sum-iv

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def combinationSum4(self, nums: List[int], target: int) -> int:
    dp = [1] + [-1] * target

    def dfs(target: int) -> int:
      if target < 0:
        return 0
      if dp[target] != -1:
        return dp …

Category: leetcode

Read More

378-Kth-Smallest-Element-In-A-Sorted-Matrix

Fri 14 November 2025

https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def kthSmallest(self, matrix: List[List[int]], k: int) -> int:
    minHeap = []  # (matrix[i][j], i, j)

    i = 0
    while i < k and i < len(matrix):
      heapq.heappush(minHeap, (matrix …

Category: leetcode

Read More
Page 35 of 146

« Prev Next »