256-Paint-House
https://leetcode.com/problems/paint-house
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minCost(self, costs: List[List[int]]) -> List[List[int]]:
for i in range(1, len(costs)):
costs[i][0] += min(costs[i - 1][1], costs[i - 1][2 …
Read More
257-Binary-Tree-Paths
https://leetcode.com/problems/binary-tree-paths
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
ans = []
def dfs(root: Optional[TreeNode], path: List[str]) -> None:
if not root:
return
if not root.left and not root …
Read More
258-Add-Digits
https://leetcode.com/problems/add-digits
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def addDigits(self, num: int) -> int:
return 0 if num == 0 else 1 + (num - 1) % 9
new Solution().addDigits()
Score: 5
Read More
259-3Sum-Smaller
https://leetcode.com/problems/3sum-smaller
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def threeSumSmaller(self, nums: List[int], target: int) -> int:
if len(nums) < 3:
return 0
ans = 0
nums.sort()
for i in range(len(nums) - 2):
l = i + 1 …
Read More
26-Dmi-And-Adx
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
26-Remove-Duplicates-From-Sorted-Array
https://leetcode.com/problems/remove-duplicates-from-sorted-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
i = 0
for num in nums:
if i < 1 or num > nums[i - 1]:
nums[i] = num
i += 1
return i
Read More
260-Single-Number-Iii
https://leetcode.com/problems/single-number-iii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
xors = functools.reduce(operator.xor, nums)
lowbit = xors & -xors
ans = [0, 0]
# Seperate nums into two groups by the lowbit
for …
Read More
261-Graph-Valid-Tree
https://leetcode.com/problems/graph-valid-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def validTree(self, n: int, edges: List[List[int]]) -> bool:
if n == 0 or len(edges) != n - 1:
return False
graph = [[] for _ in range(n)]
q = deque([0 …
Read More
263-Ugly-Number
https://leetcode.com/problems/ugly-number
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isUgly(self, n: int) -> bool:
if n == 0:
return False
for prime in 2, 3, 5:
while n % prime == 0:
n //= prime
return n == 1
Read More
264-Ugly-Number-Ii
https://leetcode.com/problems/ugly-number-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def nthUglyNumber(self, n: int) -> int:
nums = [1]
i2 = 0
i3 = 0
i5 = 0
while len(nums) < n:
next2 = nums[i2] * 2
next3 = nums[i3] * 3
next5 = nums[i5 …
Read More