46-Permutations
https://leetcode.com/problems/permutations
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
ans = []
used = [False] * len(nums)
def dfs(path: List[int]) -> None:
if len(path) == len(nums):
ans.append(path.copy …
Read More
46-Schaff-Trend-Cycle
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
462-Minimum-Moves-To-Equal-Array-Elements-Ii
https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
import statistics
class Solution:
def minMoves2(self, nums: List[int]) -> int:
median = int(statistics.median(nums))
return sum(abs(num - median) for num in nums)
new Solution().minMoves2()
Score: 5
Read More
463-Island-Perimeter
https://leetcode.com/problems/island-perimeter
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def islandPerimeter(self, grid: List[List[int]]) -> int:
m = len(grid)
n = len(grid[0])
islands = 0
neighbors = 0
for i in range(m):
for j in range(n …
Read More
465-Optimal-Account-Balancing
https://leetcode.com/problems/optimal-account-balancing
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minTransfers(self, transactions: List[List[int]]) -> int:
balance = [0] * 21
for u, v, amount in transactions:
balance[u] -= amount
balance[v] += amount
debt = [b for b in balance …
Read More
469-Convex-Polygon
https://leetcode.com/problems/convex-polygon
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isConvex(self, points: List[List[int]]) -> bool:
# Pq x qr
def getCross(p: List[int], q: List[int], r: List[int]):
return (q[0] - p[0]) * (r[1 …
Read More
47-Permutations-Ii
https://leetcode.com/problems/permutations-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
ans = []
used = [False] * len(nums)
def dfs(path: List[int]) -> None:
if len(path) == len(nums):
ans.append(path.copy …
Read More
47-Turtle-Trading-Channels
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
472-Concatenated-Words
https://leetcode.com/problems/concatenated-words
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findAllConcatenatedWordsInADict(self, words: List[str]) -> List[str]:
wordSet = set(words)
@functools.lru_cache(None)
def isConcat(word: str) -> bool:
for i in range(1, len(word)):
prefix = word[:i …
Read More
473-Matchsticks-To-Square
https://leetcode.com/problems/matchsticks-to-square
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def makesquare(self, matchsticks: List[int]) -> bool:
if len(matchsticks) < 4:
return False
perimeter = sum(matchsticks)
if perimeter % 4 != 0:
return False
A = sorted(matchsticks)[::-1]
def dfs(selected …
Read More