439-Ternary-Expression-Parser
https://leetcode.com/problems/ternary-expression-parser
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def parseTernary(self, expression: str) -> str:
c = expression[self.i]
if self.i + 1 == len(expression) or expression[self.i + 1] == ':':
self.i += 2
return str(c)
self.i …
Read More
44-Pivot-Range
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
44-Wildcard-Matching
https://leetcode.com/problems/wildcard-matching
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isMatch(self, s: str, p: str) -> bool:
m = len(s)
n = len(p)
# dp[i][j] := True if s[0..i) matches p[0..j)
dp = [[False] * (n …
Read More
441-Arranging-Coins
https://leetcode.com/problems/arranging-coins
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def arrangeCoins(self, n: int) -> int:
return int((-1 + sqrt(8 * n + 1)) // 2)
new Solution().arrangeCoins()
Score: 5
Read More
442-Find-All-Duplicates-In-An-Array
https://leetcode.com/problems/find-all-duplicates-in-an-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findDuplicates(self, nums: List[int]) -> List[int]:
ans = []
for num in nums:
nums[abs(num) - 1] *= -1
if nums[abs(num) - 1] > 0:
ans.append(abs(num))
return …
Read More
443-String-Compression
https://leetcode.com/problems/string-compression
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def compress(self, chars: List[str]) -> int:
ans = 0
i = 0
while i < len(chars):
letter = chars[i]
count = 0
while i < len(chars) and chars[i] == letter:
count …
Read More
444-Sequence-Reconstruction
https://leetcode.com/problems/sequence-reconstruction
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def sequenceReconstruction(self, org: List[int], seqs: List[List[int]]) -> bool:
if not seqs:
return False
n = len(org)
graph = [[] for _ in range(n)]
inDegree = [0] * n
# Build …
Read More
445-Add-Two-Numbers-Ii
https://leetcode.com/problems/add-two-numbers-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
stack1 = []
stack2 = []
while l1:
stack1.append(l1)
l1 = l1.next
while l2:
stack2.append(l2)
l2 = l2.next
head = None
carry …
Read More
447-Number-Of-Boomerangs
https://leetcode.com/problems/number-of-boomerangs
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numberOfBoomerangs(self, points: List[List[int]]) -> int:
ans = 0
for x1, y1 in points:
count = defaultdict(int)
for x2, y2 in points:
ans += 2 * count[(x1 - x2)**2 …
Read More
448-Find-All-Numbers-Disappeared-In-An-Array
https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
for num in nums:
index = abs(num) - 1
nums[index] = -abs(nums[index])
return [i + 1 for i, num in enumerate …
Read More