32-Longest-Valid-Parentheses
https://leetcode.com/problems/longest-valid-parentheses
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def longestValidParentheses(self, s: str) -> int:
s2 = ')' + s
# dp[i] := Length of longest valid parentheses substring of s2[1..i]
dp = [0] * len(s2)
for i in range(1 …
Read More
320-Generalized-Abbreviation
https://leetcode.com/problems/generalized-abbreviation
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def generateAbbreviations(self, word: str) -> List[str]:
ans = []
def getCountString(count: int) -> str:
return str(count) if count > 0 else ''
def dfs(i: int, count: int, path: List[str …
Read More
322-Coin-Change
https://leetcode.com/problems/coin-change
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
# dp[i] := fewest # Of coins to make up i
dp = [0] + [amount + 1] * amount
for coin in coins:
for i …
Read More
323-Number-Of-Connected-Components-In-An-Undirected-Graph
https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def countComponents(self, n: int, edges: List[List[int]]) -> int:
ans = 0
graph = [[] for _ in range(n)]
seen = set()
for u, v in edges:
graph[u].append(v …
Read More
325-Maximum-Size-Subarray-Sum-Equals-K
https://leetcode.com/problems/maximum-size-subarray-sum-equals-k
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxSubArrayLen(self, nums: List[int], k: int) -> int:
ans = 0
prefix = 0
prefixToIndex = {0: -1}
for i, num in enumerate(nums):
prefix += num
target = prefix - k
if target …
Read More
326-Power-Of-Three
https://leetcode.com/problems/power-of-three
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isPowerOfThree(self, n: int) -> bool:
return n > 0 and 3**19 % n == 0
new Solution().isPowerOfThree()
Score: 5
Read More
327-Count-Of-Range-Sum
https://leetcode.com/problems/count-of-range-sum
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def countRangeSum(self, nums: List[int], lower: int, upper: int) -> int:
n = len(nums)
self.ans = 0
prefix = [0] + list(itertools.accumulate(nums))
self._mergeSort(prefix, 0, n, lower …
Read More
328-Odd-Even-Linked-List
https://leetcode.com/problems/odd-even-linked-list
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def oddEvenList(self, head: ListNode) -> ListNode:
oddHead = ListNode(0)
evenHead = ListNode(0)
odd = oddHead
even = evenHead
isOdd = True
while head:
if isOdd:
odd.next = head
odd = head
else:
even …
Read More
329-Longest-Increasing-Path-In-A-Matrix
https://leetcode.com/problems/longest-increasing-path-in-a-matrix
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
m = len(matrix)
n = len(matrix[0])
@functools.lru_cache(None)
def dfs(i: int, j: int, prev: int) -> int:
if i …
Read More
33-Search-In-Rotated-Sorted-Array
https://leetcode.com/problems/search-in-rotated-sorted-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def search(self, nums: List[int], target: int) -> int:
l = 0
r = len(nums) - 1
while l <= r:
m = (l + r) // 2
if nums[m] == target:
return m
if …
Read More