32-Longest-Valid-Parentheses

Sat 17 May 2025

https://leetcode.com/problems/longest-valid-parentheses

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def longestValidParentheses(self, s: str) -> int:
    s2 = ')' + s
    # dp[i] := Length of longest valid parentheses substring of s2[1..i]
    dp = [0] * len(s2)

    for i in range(1 …

Category: leetcode

Read More

320-Generalized-Abbreviation

Sat 17 May 2025

https://leetcode.com/problems/generalized-abbreviation

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

    def getCountString(count: int) -> str:
      return str(count) if count > 0 else ''

    def dfs(i: int, count: int, path: List[str …

Category: leetcode

Read More

322-Coin-Change

Sat 17 May 2025

https://leetcode.com/problems/coin-change

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def coinChange(self, coins: List[int], amount: int) -> int:
    # dp[i] := fewest # Of coins to make up i
    dp = [0] + [amount + 1] * amount

    for coin in coins:
      for i …

Category: leetcode

Read More

323-Number-Of-Connected-Components-In-An-Undirected-Graph

Sat 17 May 2025

https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def countComponents(self, n: int, edges: List[List[int]]) -> int:
    ans = 0
    graph = [[] for _ in range(n)]
    seen = set()

    for u, v in edges:
      graph[u].append(v …

Category: leetcode

Read More

325-Maximum-Size-Subarray-Sum-Equals-K

Sat 17 May 2025

https://leetcode.com/problems/maximum-size-subarray-sum-equals-k

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def maxSubArrayLen(self, nums: List[int], k: int) -> int:
    ans = 0
    prefix = 0
    prefixToIndex = {0: -1}

    for i, num in enumerate(nums):
      prefix += num
      target = prefix - k
      if target …

Category: leetcode

Read More

326-Power-Of-Three

Sat 17 May 2025

https://leetcode.com/problems/power-of-three

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def isPowerOfThree(self, n: int) -> bool:
    return n > 0 and 3**19 % n == 0
new Solution().isPowerOfThree()

Score: 5

Category: leetcode

Read More

327-Count-Of-Range-Sum

Sat 17 May 2025

https://leetcode.com/problems/count-of-range-sum

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def countRangeSum(self, nums: List[int], lower: int, upper: int) -> int:
    n = len(nums)
    self.ans = 0
    prefix = [0] + list(itertools.accumulate(nums))

    self._mergeSort(prefix, 0, n, lower …

Category: leetcode

Read More

328-Odd-Even-Linked-List

Sat 17 May 2025

https://leetcode.com/problems/odd-even-linked-list

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def oddEvenList(self, head: ListNode) -> ListNode:
    oddHead = ListNode(0)
    evenHead = ListNode(0)
    odd = oddHead
    even = evenHead
    isOdd = True

    while head:
      if isOdd:
        odd.next = head
        odd = head
      else:
        even …

Category: leetcode

Read More

329-Longest-Increasing-Path-In-A-Matrix

Sat 17 May 2025

https://leetcode.com/problems/longest-increasing-path-in-a-matrix

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

    @functools.lru_cache(None)
    def dfs(i: int, j: int, prev: int) -> int:
      if i …

Category: leetcode

Read More

33-Search-In-Rotated-Sorted-Array

Sat 17 May 2025

https://leetcode.com/problems/search-in-rotated-sorted-array

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def search(self, nums: List[int], target: int) -> int:
    l = 0
    r = len(nums) - 1

    while l <= r:
      m = (l + r) // 2
      if nums[m] == target:
        return m
      if …

Category: leetcode

Read More
Page 27 of 77

« Prev Next »