3-Longest-Substring-Without-Repeating-Characters

Sat 17 May 2025

https://leetcode.com/problems/longest-substring-without-repeating-characters

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def lengthOfLongestSubstring(self, s: str) -> int:
    ans = 0
    count = Counter()

    l = 0
    for r, c in enumerate(s):
      count[c] += 1
      while count[c] > 1:
        count[s[l]] -= 1 …

Category: leetcode

Read More

3-Longest-Substring-Without-Repeating-Chars

Sat 17 May 2025
# https://leetcode.com/problems/longest-substring-without-repeating-characters/
import pyutil as pyu
pyu.get_local_pyinfo()
'conda env: ml311; pyv: 3.11.10 (main, Oct  3 2024, 07:29:13) [GCC 11.2.0]'
print(pyu.ps2("scipy"))
scipy==1.14.1

from collections import Counter

class Solution:
  def lengthOfLongestSubstring(self, s: str) -> int:
    ans …

Category: leetcode

Read More

30-Substring-With-Concatenation-Of-All-Words

Sat 17 May 2025

https://leetcode.com/problems/substring-with-concatenation-of-all-words

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findSubstring(self, s: str, words: List[str]) -> List[int]:
    if len(s) == 0 or words == []:
      return []

    k = len(words)
    n = len(words[0])
    ans = []
    count = Counter(words)

    for …

Category: leetcode

Read More

300-Longest-Increasing-Subsequence

Sat 17 May 2025

https://leetcode.com/problems/longest-increasing-subsequence

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

    # dp[i] := LIS ending at nums[i]
    dp = [1] * len(nums)

    for i in range(1, len …

Category: leetcode

Read More

301-Remove-Invalid-Parentheses

Sat 17 May 2025

https://leetcode.com/problems/remove-invalid-parentheses

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def removeInvalidParentheses(self, s: str) -> List[str]:
    def getLeftAndRightCounts(s: str) -> tuple:
      l = 0
      r = 0

      for c in s:
        if c == '(':
          l += 1
        elif c == ')':
          if l == 0 …

Category: leetcode

Read More

302-Smallest-Rectangle-Enclosing-Black-Pixels

Sat 17 May 2025

https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def minArea(self, image: List[List[str]], x: int, y: int) -> int:
    m = len(image)
    n = len(image[0])
    dirs = [0, 1, 0, -1, 0]
    topLeft = [x, y]
    bottomRight …

Category: leetcode

Read More

305-Number-Of-Islands-Ii

Sat 17 May 2025

https://leetcode.com/problems/number-of-islands-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class UnionFind:
  def __init__(self, n: int):
    self.id = [-1] * n

  def union(self, u: int, v: int) -> None:
    self.id[u] = v

  def find(self, u: int) -> int:
    if self …

Category: leetcode

Read More

306-Additive-Number

Sat 17 May 2025

https://leetcode.com/problems/additive-number

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

    def dfs(firstNum: int, secondNum: int, s: int) -> bool:
      if s == len(num):
        return True

      thirdNum = firstNum + secondNum
      thirdNumStr = str …

Category: leetcode

Read More

309-Best-Time-To-Buy-And-Sell-Stock-With-Cooldown

Sat 17 May 2025

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def maxProfit(self, prices: List[int]) -> int:
    sell = 0
    hold = -math.inf
    prev = 0

    for price in prices:
      cache = sell
      sell = max(sell, hold + price)
      hold = max(hold, prev …

Category: leetcode

Read More

31-Next-Permutation

Sat 17 May 2025

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

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

    # From back to front, find the first num < nums[i + 1]
    i = n - 2
    while i >= 0:
      if nums …

Category: leetcode

Read More
Page 25 of 77

« Prev Next »