152-Maximum-Product-Subarray

Sat 17 May 2025

https://leetcode.com/problems/maximum-product-subarray

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

    for i in range(1, len(nums)):
      mini = prevMin * nums[i]
      maxi = prevMax …

Category: leetcode

Read More

153-Find-Minimum-In-Rotated-Sorted-Array

Sat 17 May 2025

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

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

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

Category: leetcode

Read More

154-Find-Minimum-In-Rotated-Sorted-Array-Ii

Sat 17 May 2025

https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii

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

    while l < r:
      m = (l + r) // 2
      if nums[m] == nums[r]:
        r -= 1
      elif nums …

Category: leetcode

Read More

156-Binary-Tree-Upside-Down

Sat 17 May 2025

https://leetcode.com/problems/binary-tree-upside-down

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def upsideDownBinaryTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
    if not root or not root.left:
      return root

    newRoot = self.upsideDownBinaryTree(root.left)
    root.left.left = root.right  # 2's …

Category: leetcode

Read More

157-Read-N-Characters-Given-Read4

Sat 17 May 2025

https://leetcode.com/problems/read-n-characters-given-read4

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
"""
The read4 API is already defined for you.
    def read4(buf4: List[chr]) -> int:

# Below is an example of how the read4 API can be called.
file = File("abcdefghijk") # File is …

Category: leetcode

Read More

158-Read-N-Characters-Given-Read4-Ii-Call-Multiple-Times

Sat 17 May 2025

https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
# The read4 API is already defined for you.
# Def read4(buf4: List[str]) -> int:

class Solution:
  def read(self, buf: List[str], n: int) -> int:
    i = 0  # buf's index

    while …

Category: leetcode

Read More

159-Longest-Substring-With-At-Most-Two-Distinct-Characters

Sat 17 May 2025

https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def lengthOfLongestSubstringTwoDistinct(self, s: str) -> int:
    ans = 0
    distinct = 0
    count = [0] * 128

    l = 0
    for r, c in enumerate(s):
      count[ord(c)] += 1
      if count[ord(c …

Category: leetcode

Read More

16-3Sum-Closest

Sat 17 May 2025

https://leetcode.com/problems/3sum-closest

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

    nums.sort()

    for i in range(len(nums) - 2):
      if i > 0 and …

Category: leetcode

Read More

160-Intersection-Of-Two-Linked-Lists

Sat 17 May 2025

https://leetcode.com/problems/intersection-of-two-linked-lists

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
    a = headA
    b = headB

    while a != b:
      a = a.next if a else headB
      b = b.next if b else …

Category: leetcode

Read More

161-One-Edit-Distance

Sat 17 May 2025

https://leetcode.com/problems/one-edit-distance

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def isOneEditDistance(self, s: str, t: str) -> bool:
    m = len(s)
    n = len(t)
    if m > n:  # Make sure len(s) <= len(t)
      return self.isOneEditDistance(t, s)

    for …

Category: leetcode

Read More
Page 12 of 77

« Prev Next »