152-Maximum-Product-Subarray
https://leetcode.com/problems/maximum-product-subarray
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
153-Find-Minimum-In-Rotated-Sorted-Array
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
154-Find-Minimum-In-Rotated-Sorted-Array-Ii
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
156-Binary-Tree-Upside-Down
https://leetcode.com/problems/binary-tree-upside-down
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
157-Read-N-Characters-Given-Read4
https://leetcode.com/problems/read-n-characters-given-read4
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
"""
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 …
Read More
158-Read-N-Characters-Given-Read4-Ii-Call-Multiple-Times
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"))
# 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 …
Read More
159-Longest-Substring-With-At-Most-Two-Distinct-Characters
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"))
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 …
Read More
16-3Sum-Closest
https://leetcode.com/problems/3sum-closest
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
160-Intersection-Of-Two-Linked-Lists
https://leetcode.com/problems/intersection-of-two-linked-lists
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
161-One-Edit-Distance
https://leetcode.com/problems/one-edit-distance
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More