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
522-Longest-Uncommon-Subsequence-Ii
https://leetcode.com/problems/longest-uncommon-subsequence-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findLUSlength(self, strs: List[str]) -> int:
def isSubsequence(a: str, b: str) -> bool:
i = 0
j = 0
while i < len(a) and j < len(b):
if a[i …
Read More
523-Continuous-Subarray-Sum
https://leetcode.com/problems/continuous-subarray-sum
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def checkSubarraySum(self, nums: List[int], k: int) -> bool:
prefix = 0
prefixToIndex = {0: -1}
for i, num in enumerate(nums):
prefix += num
if k != 0:
prefix %= k
if prefix …
Read More
524-Longest-Word-In-Dictionary-Through-Deleting
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findLongestWord(self, s: str, d: List[str]) -> str:
ans = ''
for word in d:
i = 0
for c in s:
if i < len(word) and c == word[i]:
i …
Read More