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
67-Add-Binary
https://leetcode.com/problems/add-binary
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def addBinary(self, a: str, b: str) -> str:
s = []
carry = 0
i = len(a) - 1
j = len(b) - 1
while i >= 0 or j >= 0 or carry:
if i …
Read More
670-Maximum-Swap
https://leetcode.com/problems/maximum-swap
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maximumSwap(self, num: int) -> int:
s = list(str(num))
dict = {c: i for i, c in enumerate(s)}
for i, c in enumerate(s):
for digit in reversed …
Read More
672-Bulb-Switcher-Ii
https://leetcode.com/problems/bulb-switcher-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def flipLights(self, n: int, m: int) -> int:
n = min(n, 3)
if m == 0:
return 1
if m == 1:
return [2, 3, 4][n - 1]
if m == 2 …
Read More
673-Number-Of-Longest-Increasing-Subsequence
https://leetcode.com/problems/number-of-longest-increasing-subsequence
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findNumberOfLIS(self, nums: List[int]) -> int:
ans = 0
maxLength = 0
length = [1] * len(nums) # length[i] := LIS's length ending w/ nums[i]
count = [1] * len(nums) # count …
Read More