42-Trapping-Rain-Water
https://leetcode.com/problems/trapping-rain-water
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def trap(self, height: List[int]) -> int:
n = len(height)
l = [0] * n # l[i] := max(height[0..i])
r = [0] * n # r[i] := max(height[i..n))
for …
Read More
423-Reconstruct-Original-Digits-From-English
https://leetcode.com/problems/reconstruct-original-digits-from-english
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def originalDigits(self, s: str) -> str:
count = [0] * 10
for c in s:
if c == 'z':
count[0] += 1
if c == 'o':
count[1] += 1
if c == 'w':
count …
Read More
424-Longest-Repeating-Character-Replacement
https://leetcode.com/problems/longest-repeating-character-replacement
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
ans = 0
maxCount = 0
count = Counter()
l = 0
for r, c in enumerate(s):
count[c] += 1
maxCount = max(maxCount, count …
Read More
43-Multiply-Strings
https://leetcode.com/problems/multiply-strings
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def multiply(self, num1: str, num2: str) -> str:
s = [0] * (len(num1) + len(num2))
for i in reversed(range(len(num1))):
for j in reversed(range(len(num2))):
mult …
Read More
43-Volume-Weighted-Macd
import pyutil as pyu
pyu.get_local_pyinfo()
'conda env: ml312-2024; pyv: 3.12.7 | packaged by Anaconda, Inc. | (main, Oct 4 2024, 13:27:36) [GCC 11.2.0]'
print(pyu.ps2("yfinance pandas matplotlib"))
yfinance==0.2.51
pandas==2.2.3
matplotlib==3.9.3
Read More
430-Flatten-A-Multilevel-Doubly-Linked-List
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def flatten(self, head: 'Node') -> 'Node':
def flatten(head: 'Node', rest: 'Node') -> 'Node':
if not head:
return rest
head.next = flatten(head.child, flatten(head.next, rest))
if head …
Read More
434-Number-Of-Segments-In-A-String
https://leetcode.com/problems/number-of-segments-in-a-string
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def countSegments(self, s: str) -> int:
return len(s.split())
new Solution().countSegments()
Score: 5
Read More
435-Non-Overlapping-Intervals
https://leetcode.com/problems/non-overlapping-intervals
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
ans = 0
currentEnd = -math.inf
for interval in sorted(intervals, key=lambda x: x[1]):
if interval[0] >= currentEnd:
currentEnd = interval …
Read More
437-Path-Sum-Iii
https://leetcode.com/problems/path-sum-iii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def pathSum(self, root: TreeNode, summ: int) -> int:
if not root:
return 0
def dfs(root: TreeNode, summ: int) -> int:
if not root:
return 0
return (summ == root.val …
Read More
438-Find-All-Anagrams-In-A-String
https://leetcode.com/problems/find-all-anagrams-in-a-string
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
ans = []
count = Counter(p)
required = len(p)
for r, c in enumerate(s):
count[c] -= 1
if count[c] >= 0 …
Read More