713-Subarray-Product-Less-Than-K
https://leetcode.com/problems/subarray-product-less-than-k
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int:
if k <= 1:
return 0
ans = 0
prod = 1
j = 0
for i, num in enumerate(nums):
prod *= num
while …
Read More
714-Best-Time-To-Buy-And-Sell-Stock-With-Transaction-Fee
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxProfit(self, prices: List[int], fee: int) -> int:
sell = 0
hold = -math.inf
for price in prices:
sell = max(sell, hold + price)
hold = max(hold, sell - price - fee …
Read More
717-1-Bit-And-2-Bit-Characters
https://leetcode.com/problems/1-bit-and-2-bit-characters
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isOneBitCharacter(self, bits: List[int]) -> bool:
i = 0
while i < len(bits) - 1:
i += bits[i] + 1
return i == len(bits) - 1
new Solution().isOneBitCharacter()
Score: 5
Read More
718-Maximum-Length-Of-Repeated-Subarray
https://leetcode.com/problems/maximum-length-of-repeated-subarray
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
m = len(nums1)
n = len(nums2)
ans = 0
# dp[i][j] := max length of nums1[i:] and nums2[j …
Read More
719-Find-K-Th-Smallest-Pair-Distance
https://leetcode.com/problems/find-k-th-smallest-pair-distance
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def smallestDistancePair(self, nums: List[int], k: int) -> int:
nums.sort()
l = 0
r = nums[-1] - nums[0]
while l < r:
m = (l + r) // 2
count = 0
j = 0 …
Read More
72-Edit-Distance
https://leetcode.com/problems/edit-distance
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minDistance(self, word1: str, word2: str) -> int:
m = len(word1)
n = len(word2)
# dp[i][j] := min # Of operations to convert word1[0..i) to word2[0..j …
Read More
720-Longest-Word-In-Dictionary
https://leetcode.com/problems/longest-word-in-dictionary
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def longestWord(self, words: List[str]) -> str:
root = {}
for word in words:
node = root
for c in word:
if c not in node:
node[c] = {}
node = node[c]
node …
Read More
722-Remove-Comments
https://leetcode.com/problems/remove-comments
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def removeComments(self, source: List[str]) -> List[str]:
ans = []
commenting = False
modified = ''
for line in source:
i = 0
while i < len(line):
if i + 1 == len(line):
if not …
Read More
724-Find-Pivot-Index
https://leetcode.com/problems/find-pivot-index
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def pivotIndex(self, nums: List[int]) -> int:
summ = sum(nums)
prefix = 0
for i, num in enumerate(nums):
if prefix == summ - prefix - num:
return i
prefix += num
return -1 …
Read More
725-Split-Linked-List-In-Parts
https://leetcode.com/problems/split-linked-list-in-parts
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def splitListToParts(self, root: ListNode, k: int) -> List[ListNode]:
ans = [[] for _ in range(k)]
length = 0
curr = root
while curr:
length += 1
curr = curr.next
subLength = length // k …
Read More