237-Delete-Node-In-A-Linked-List

Fri 14 November 2025

https://leetcode.com/problems/delete-node-in-a-linked-list

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def deleteNode(self, node):
    node.val = node.next.val
    node.next = node.next.next
new Solution().deleteNode()

Score: 5

Category: leetcode

Read More

238-Product-Of-Array-Except-Self

Fri 14 November 2025

https://leetcode.com/problems/product-of-array-except-self

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def productExceptSelf(self, nums: List[int]) -> List[int]:
    n = len(nums)
    prefix = [1] * n  # Prefix product
    suffix = [1] * n  # Suffix product

    for i in range(1, n):
      prefix[i …

Category: leetcode

Read More

239-Sliding-Window-Maximum

Fri 14 November 2025

https://leetcode.com/problems/sliding-window-maximum

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
    ans = []
    q = deque()  # Max queue

    for i, num in enumerate(nums):
      while q and q[-1] < num:
        q.pop …

Category: leetcode

Read More

24-Swap-Nodes-In-Pairs

Fri 14 November 2025

https://leetcode.com/problems/swap-nodes-in-pairs

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def swapPairs(self, head: ListNode) -> ListNode:
    def getLength(head: ListNode) -> int:
      length = 0
      while head:
        length += 1
        head = head.next
      return length

    length = getLength(head)
    dummy = ListNode(0, head …

Category: leetcode

Read More

240-Search-A-2D-Matrix-Ii

Fri 14 November 2025

https://leetcode.com/problems/search-a-2d-matrix-ii

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

    while r < len(matrix) and c >= 0:
      if matrix[r][c] == target …

Category: leetcode

Read More

241-Different-Ways-To-Add-Parentheses

Fri 14 November 2025

https://leetcode.com/problems/different-ways-to-add-parentheses

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  @functools.lru_cache(None)
  def diffWaysToCompute(self, expression: str) -> List[int]:
    ans = []

    for i, c in enumerate(expression):
      if c in '+-*':
        for a in self.diffWaysToCompute(expression[:i]):
          for b …

Category: leetcode

Read More

242-Valid-Anagram

Fri 14 November 2025

https://leetcode.com/problems/valid-anagram

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def isAnagram(self, s: str, t: str) -> bool:
    if len(s) != len(t):
      return False

    dict = Counter(s)

    for c in t:
      dict[c] -= 1
      if dict[c] < 0 …

Category: leetcode

Read More

243-Shortest-Word-Distance

Fri 14 November 2025

https://leetcode.com/problems/shortest-word-distance

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def shortestDistance(self, wordsDict: List[str], word1: str, word2: str) -> int:
    ans = len(wordsDict)
    index1 = -1  # wordsDict[index1] == word1
    index2 = -1  # wordsDict[index2] == word2

    for i, word in enumerate …

Category: leetcode

Read More

245-Shortest-Word-Distance-Iii

Fri 14 November 2025

https://leetcode.com/problems/shortest-word-distance-iii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def shortestWordDistance(self, wordsDict: List[str], word1: str, word2: str) -> int:
    isSame = word1 == word2
    ans = math.inf
    # If word1 == word2, index1 is the newest index
    index1 = len(wordsDict)
    # If …

Category: leetcode

Read More

246-Strobogrammatic-Number

Fri 14 November 2025

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

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def isStrobogrammatic(self, num: str) -> bool:
    rotated = ['0', '1', 'x', 'x', 'x',
               'x', '9', 'x', '8', '6']
    l = 0
    r = len(num) - 1

    while l <= r:
      if num[l …

Category: leetcode

Read More
Page 19 of 77

« Prev Next »