51-N-Queens
https://leetcode.com/problems/n-queens
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def solveNQueens(self, n: int) -> List[List[str]]:
ans = []
cols = [False] * n
diag1 = [False] * (2 * n - 1)
diag2 = [False] * (2 * n - 1)
def dfs(i: int, board: List[int …
Read More
513-Find-Bottom-Left-Tree-Value
https://leetcode.com/problems/find-bottom-left-tree-value
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
q = deque([root])
while q:
root = q.popleft()
if root.right:
q.append(root.right)
if root.left:
q.append(root.left …
Read More
514-Freedom-Trail
https://leetcode.com/problems/freedom-trail
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findRotateSteps(self, ring: str, key: str) -> int:
# Number of rotates of ring to match key[index:]
@functools.lru_cache(None)
def dfs(ring: str, index: int) -> int:
if index …
Read More
515-Find-Largest-Value-In-Each-Tree-Row
https://leetcode.com/problems/find-largest-value-in-each-tree-row
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def largestValues(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return []
ans = []
q = deque([root])
while q:
maxi = -math.inf
for _ in range(len(q)):
root = q …
Read More
516-Longest-Palindromic-Subsequence
https://leetcode.com/problems/longest-palindromic-subsequence
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def longestPalindromeSubseq(self, s: str) -> int:
# Dp(i, j) := LPS's length in s[i..j]
@functools.lru_cache(None)
def dp(i: int, j: int) -> int:
if i > j …
Read More
517-Super-Washing-Machines
https://leetcode.com/problems/super-washing-machines
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findMinMoves(self, machines: List[int]) -> int:
dresses = sum(machines)
if dresses % len(machines) != 0:
return -1
ans = 0
average = dresses // len(machines)
inout = 0
for dress in machines …
Read More
518-Coin-Change-Ii
https://leetcode.com/problems/coin-change-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def change(self, amount: int, coins: List[int]) -> int:
dp = [1] + [0] * amount
for coin in coins:
for i in range(coin, amount + 1):
dp[i] += dp[i - coin …
Read More
52-N-Queens-Ii
https://leetcode.com/problems/n-queens-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def totalNQueens(self, n: int) -> int:
ans = 0
cols = [False] * n
diag1 = [False] * (2 * n - 1)
diag2 = [False] * (2 * n - 1)
def dfs(i: int) -> None:
nonlocal ans
if …
Read More
520-Detect-Capital
https://leetcode.com/problems/detect-capital
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def detectCapitalUse(self, word: str) -> bool:
return word.isupper() or word.islower() or word.istitle()
new Solution().detectCapitalUse()
Score: 5
Read More
521-Longest-Uncommon-Subsequence-I
https://leetcode.com/problems/longest-uncommon-subsequence-i
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findLUSlength(self, a: str, b: str) -> int:
return -1 if a == b else max(len(a), len(b))
new Solution().findLUSlength()
Score: 5
Read More