656-Coin-Path
https://leetcode.com/problems/coin-path
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def cheapestJump(self, coins: List[int], maxJump: int) -> List[int]:
if coins[-1] == -1:
return []
n = len(coins)
# dp[i] := min cost to jump from i to n - 1 …
Read More
657-Robot-Return-To-Origin
https://leetcode.com/problems/robot-return-to-origin
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def judgeCircle(self, moves: str) -> bool:
return moves.count('R') == moves.count('L') and moves.count('U') == moves.count('D')
new Solution().judgeCircle()
Score: 5
Read More
658-Find-K-Closest-Elements
https://leetcode.com/problems/find-k-closest-elements
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:
l = 0
r = len(arr) - k
while l < r:
m = (l + r) // 2
if x - arr[m …
Read More
66-Plus-One
https://leetcode.com/problems/plus-one
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
for i, d in reversed(list(enumerate(digits))):
if d < 9:
digits[i] += 1
return digits
digits[i] = 0
return [1 …
Read More
660-Remove-9
https://leetcode.com/problems/remove-9
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def newInteger(self, n: int) -> int:
ans = []
while n:
ans.append(str(n % 9))
n //= 9
return ''.join(reversed(ans))
new Solution().newInteger()
Score: 5
Read More
661-Image-Smoother
https://leetcode.com/problems/image-smoother
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def imageSmoother(self, M: List[List[int]]) -> List[List[int]]:
m = len(M)
n = len(M[0])
ans = [[0 for j in range(n)] for i in range(m …
Read More
663-Equal-Tree-Partition
https://leetcode.com/problems/equal-tree-partition
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def checkEqualTree(self, root: Optional[TreeNode]) -> bool:
if not root:
return False
seen = set()
def dfs(root: Optional[TreeNode]) -> int:
if not root:
return 0
summ = root.val + dfs …
Read More
665-Non-Decreasing-Array
https://leetcode.com/problems/non-decreasing-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def checkPossibility(self, nums: List[int]) -> bool:
j = None
for i in range(len(nums) - 1):
if nums[i] > nums[i + 1]:
if j is not None:
return False …
Read More
666-Path-Sum-Iv
https://leetcode.com/problems/path-sum-iv
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def pathSum(self, nums: List[int]) -> int:
ans = 0
tree = [[-1] * 8 for _ in range(4)]
for num in nums:
d = num // 100 - 1
p = (num % 100) // 10 …
Read More
667-Beautiful-Arrangement-Ii
https://leetcode.com/problems/beautiful-arrangement-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def constructArray(self, n: int, k: int) -> List[int]:
ans = list(range(1, n - k + 1))
for i in range(k):
if i % 2 == 0:
ans.append(n - i …
Read More