61-Rotate-List
https://leetcode.com/problems/rotate-list
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def rotateRight(self, head: ListNode, k: int) -> ListNode:
if not head or not head.next or k == 0:
return head
tail = head
length = 1
while tail.next:
tail = tail …
Read More
611-Valid-Triangle-Number
https://leetcode.com/problems/valid-triangle-number
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def triangleNumber(self, nums: List[int]) -> int:
ans = 0
nums.sort()
for k in range(len(nums) - 1, 1, -1):
i = 0
j = k - 1
while i < j:
if …
Read More
616-Add-Bold-Tag-In-String
https://leetcode.com/problems/add-bold-tag-in-string
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def addBoldTag(self, s: str, words: List[str]) -> str:
n = len(s)
ans = []
# bold[i] := True if s[i] should be bolded
bold = [0] * n
boldEnd = -1 # s[i …
Read More
617-Merge-Two-Binary-Trees
https://leetcode.com/problems/merge-two-binary-trees
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if not root1 and not root2:
return None
val = (root1.val if root1 else 0) + (root2.val if …
Read More
62-Unique-Paths
https://leetcode.com/problems/unique-paths
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
# dp[i][j] := unique paths from (0, 0) to (i, j)
dp = [[1] * n for _ in range(m)]
for i …
Read More
621-Task-Scheduler
https://leetcode.com/problems/task-scheduler
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
count = Counter(tasks)
maxFreq = max(count.values())
# Put the most frequent task in the slot first
maxFreqTaskOccupy = (maxFreq - 1) * (n …
Read More
624-Maximum-Distance-In-Arrays
https://leetcode.com/problems/maximum-distance-in-arrays
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxDistance(self, arrays: List[List[int]]) -> int:
ans = 0
mini = 10000
maxi = -10000
for A in arrays:
ans = max(ans, A[-1] - mini, maxi - A[0])
mini = min …
Read More
625-Minimum-Factorization
https://leetcode.com/problems/minimum-factorization
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def smallestFactorization(self, num: int) -> int:
if num == 1:
return 1
ans = 0
base = 1
for i in range(9, 1, -1):
while num % i == 0:
num //= i
ans …
Read More
628-Maximum-Product-Of-Three-Numbers
https://leetcode.com/problems/maximum-product-of-three-numbers
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums.sort()
return max(nums[-1] * nums[0] * nums[1],
nums[-1] * nums[-2] * nums[-3])
new Solution().maximumProduct()
Score: 5
Read More
629-K-Inverse-Pairs-Array
https://leetcode.com/problems/k-inverse-pairs-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def kInversePairs(self, n: int, k: int) -> int:
kMod = 1_000_000_007
# dp[i][j] := # Of permutations of numbers 1..i with j inverse pairs
dp = [[0] * (k + 1) for …
Read More