365-Water-And-Jug-Problem
https://leetcode.com/problems/water-and-jug-problem
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def canMeasureWater(self, jug1Capacity: int, jug2Capacity: int, targetCapacity: int) -> bool:
return targetCapacity == 0 or \
jug1Capacity + jug2Capacity >= targetCapacity and \
targetCapacity % gcd(jug1Capacity, jug2Capacity) == 0
new Solution().canMeasureWater()
Score: 5
Read More
367-Valid-Perfect-Square
https://leetcode.com/problems/valid-perfect-square
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isPerfectSquare(self, num: int) -> bool:
l = 1
r = num
while l < r:
m = (l + r) // 2
if m >= num / m:
r = m
else:
l = m + 1
return l …
Read More
368-Largest-Divisible-Subset
https://leetcode.com/problems/largest-divisible-subset
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def largestDivisibleSubset(self, nums: List[int]) -> List[int]:
n = len(nums)
ans = []
count = [1] * n
prevIndex = [-1] * n
maxCount = 0
index = -1
nums.sort()
for i, num in enumerate …
Read More
37-Relative-Vigor-Index
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
37-Sudoku-Solver
https://leetcode.com/problems/sudoku-solver
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def solveSudoku(self, board: List[List[str]]) -> None:
def isValid(row: int, col: int, c: chr) -> bool:
for i in range(9):
if board[i][col] == c or \
board …
Read More
371-Sum-Of-Two-Integers
https://leetcode.com/problems/sum-of-two-integers
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def getSum(self, a: int, b: int) -> int:
mask = 0xFFFFFFFF
kMax = 2000
while b:
a, b = (a ^ b) & mask, ((a & b) << 1) & mask
return a if a < kMax else …
Read More
372-Super-Pow
https://leetcode.com/problems/super-pow
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def superPow(self, a: int, b: List[int]) -> int:
def powMod(x: int, y: int) -> int:
pow = 1
for _ in range(y):
pow = (pow * x) % k
return pow …
Read More
375-Guess-Number-Higher-Or-Lower-Ii
https://leetcode.com/problems/guess-number-higher-or-lower-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def getMoneyAmount(self, n: int) -> int:
# Dp(i, j) := min money you need to guarantee a win of picking i..j
@functools.lru_cache(None)
def dp(i: int, j …
Read More
377-Combination-Sum-Iv
https://leetcode.com/problems/combination-sum-iv
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def combinationSum4(self, nums: List[int], target: int) -> int:
dp = [1] + [-1] * target
def dfs(target: int) -> int:
if target < 0:
return 0
if dp[target] != -1:
return dp …
Read More
378-Kth-Smallest-Element-In-A-Sorted-Matrix
https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def kthSmallest(self, matrix: List[List[int]], k: int) -> int:
minHeap = [] # (matrix[i][j], i, j)
i = 0
while i < k and i < len(matrix):
heapq.heappush(minHeap, (matrix …
Read More