487-Max-Consecutive-Ones-Ii

Sat 17 May 2025

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

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
    zeros = 0

    l = 0
    for r, num in enumerate(nums):
      if num == 0:
        zeros += 1
      while zeros == 2:
        if nums …

Category: leetcode

Read More

488-Zuma-Game

Sat 17 May 2025

https://leetcode.com/problems/zuma-game

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findMinStep(self, board: str, hand: str) -> int:
    def deDup(board):
      start = 0  # Start index of a color sequenece
      for i, c in enumerate(board):
        if c != board[start …

Category: leetcode

Read More

49-Group-Anagrams

Sat 17 May 2025

https://leetcode.com/problems/group-anagrams

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
    dict = defaultdict(list)

    for str in strs:
      key = ''.join(sorted(str))
      dict[key].append(str)

    return dict.values()
new Solution …

Category: leetcode

Read More

490-The-Maze

Sat 17 May 2025

https://leetcode.com/problems/the-maze

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def hasPath(self, maze: List[List[int]], start: List[int], destination: List[int]) -> bool:
    m = len(maze)
    n = len(maze[0])
    dirs = [0, 1, 0, -1, 0]
    q = deque …

Category: leetcode

Read More

491-Increasing-Subsequences

Sat 17 May 2025

https://leetcode.com/problems/increasing-subsequences

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

    def dfs(s: int, path: List[int]) -> None:
      if len(path) > 1:
        ans.append(path)

      used = set()

      for i …

Category: leetcode

Read More

494-Target-Sum

Sat 17 May 2025

https://leetcode.com/problems/target-sum

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findTargetSumWays(self, nums: List[int], target: int) -> int:
    summ = sum(nums)
    if summ < abs(target) or (summ + target) & 1:
      return 0

    def knapsack(target: int) -> int:
      # dp[i …

Category: leetcode

Read More

495-Teemo-Attacking

Sat 17 May 2025

https://leetcode.com/problems/teemo-attacking

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
    if duration == 0:
      return 0

    ans = 0

    for i in range(0, len(timeSeries) - 1):
      ans += min(timeSeries[i + 1 …

Category: leetcode

Read More

496-Next-Greater-Element-I

Sat 17 May 2025

https://leetcode.com/problems/next-greater-element-i

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
    numToNextGreater = {}
    stack = []

    for num in nums2:
      while stack and stack[-1] < num:
        numToNextGreater[stack.pop()] = num
      stack.append …

Category: leetcode

Read More

497-Random-Point-In-Non-Overlapping-Rectangles

Sat 17 May 2025

https://leetcode.com/problems/random-point-in-non-overlapping-rectangles

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def __init__(self, rects: List[List[int]]):
    self.rects = rects
    self.areas = list(itertools.accumulate(
        [(x2 - x1 + 1) * (y2 - y1 + 1) for x1, y1, x2, y2 in rects]))

  def …

Category: leetcode

Read More

499-The-Maze-Iii

Sat 17 May 2025

https://leetcode.com/problems/the-maze-iii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findShortestWay(self, maze: List[List[int]], ball: List[int], hole: List[int]) -> str:
    ans = "impossible"
    minSteps = math.inf

    def dfs(i: int, j: int, dx: int, dy: int …

Category: leetcode

Read More
Page 38 of 77

« Prev Next »