47-Permutations-Ii

Sat 17 May 2025

https://leetcode.com/problems/permutations-ii

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

    def dfs(path: List[int]) -> None:
      if len(path) == len(nums):
        ans.append(path.copy …

Category: leetcode

Read More

472-Concatenated-Words

Sat 17 May 2025

https://leetcode.com/problems/concatenated-words

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

    @functools.lru_cache(None)
    def isConcat(word: str) -> bool:
      for i in range(1, len(word)):
        prefix = word[:i …

Category: leetcode

Read More

473-Matchsticks-To-Square

Sat 17 May 2025

https://leetcode.com/problems/matchsticks-to-square

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def makesquare(self, matchsticks: List[int]) -> bool:
    if len(matchsticks) < 4:
      return False

    perimeter = sum(matchsticks)
    if perimeter % 4 != 0:
      return False

    A = sorted(matchsticks)[::-1]

    def dfs(selected …

Category: leetcode

Read More

474-Ones-And-Zeroes

Sat 17 May 2025

https://leetcode.com/problems/ones-and-zeroes

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
    # dp[i][j] := max size of the subset given i 0's and j 1's are available …

Category: leetcode

Read More

478-Generate-Random-Point-In-A-Circle

Sat 17 May 2025

https://leetcode.com/problems/generate-random-point-in-a-circle

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def __init__(self, radius: float, x_center: float, y_center: float):
    self.radius = radius
    self.x_center = x_center
    self.y_center = y_center

  def randPoint(self) -> List[float]:
    length = sqrt(random.uniform(0, 1 …

Category: leetcode

Read More

479-Largest-Palindrome-Product

Sat 17 May 2025

https://leetcode.com/problems/largest-palindrome-product

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

    kMod = 1337
    upper = pow(10, n) - 1
    lower = pow(10, n - 1) - 1

    for i in range(upper …

Category: leetcode

Read More

48-Rotate-Image

Sat 17 May 2025

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

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def rotate(self, matrix: List[List[int]]) -> None:
    matrix.reverse()

    for i in range(len(matrix)):
      for j in range(i + 1, len(matrix)):
        matrix[i][j], matrix[j …

Category: leetcode

Read More

483-Smallest-Good-Base

Sat 17 May 2025

https://leetcode.com/problems/smallest-good-base

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def smallestGoodBase(self, n: str) -> str:
    n = int(n)

    for m in range(int(math.log(n, 2)), 1, -1):
      k = int(n**m**-1)
      if (k**(m + 1 …

Category: leetcode

Read More

484-Find-Permutation

Sat 17 May 2025

https://leetcode.com/problems/find-permutation

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

    for i, c in enumerate(s):
      stack.append(i + 1)
      if c == 'I':
        while stack:  # Consume all decreasings
          ans.append …

Category: leetcode

Read More

485-Max-Consecutive-Ones

Sat 17 May 2025

https://leetcode.com/problems/max-consecutive-ones

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

    for num in nums:
      if num == 0:
        summ = 0
      else:
        summ += num
        ans = max(ans, summ)

    return ans …

Category: leetcode

Read More
Page 37 of 77

« Prev Next »