697-Degree-Of-An-Array
https://leetcode.com/problems/degree-of-an-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findShortestSubArray(self, nums: List[int]) -> int:
ans = 0
degree = 0
debut = {}
count = Counter()
for i, num in enumerate(nums):
debut.setdefault(num, i)
count[num] += 1
if count …
Read More
7-Reverse-Integer
https://leetcode.com/problems/reverse-integer
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def reverse(self, x: int) -> int:
ans = 0
sign = -1 if x < 0 else 1
x *= sign
while x:
ans = ans * 10 + x % 10
x //= 10
return 0 if …
Read More
70-Climbing-Stairs
https://leetcode.com/problems/climbing-stairs
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def climbStairs(self, n: int) -> int:
# dp[i] := # Of distinct ways to climb to i-th stair
dp = [1, 1] + [0] * (n - 1)
for i in range(2, n …
Read More
700-Search-In-A-Binary-Search-Tree
https://leetcode.com/problems/search-in-a-binary-search-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
if not root:
return None
if root.val == val:
return root
if root.val > val:
return self.searchBST(root …
Read More
701-Insert-Into-A-Binary-Search-Tree
https://leetcode.com/problems/insert-into-a-binary-search-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
if not root:
return TreeNode(val)
if root.val > val:
root.left = self.insertIntoBST(root.left, val)
else:
root …
Read More
702-Search-In-A-Sorted-Array-Of-Unknown-Size
https://leetcode.com/problems/search-in-a-sorted-array-of-unknown-size
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
# """
# This is ArrayReader's API interface.
# You should not implement it, or speculate about its implementation
# """
# Class ArrayReader:
# def get(self, index: int) -> int:
class Solution:
def search(self, reader: 'ArrayReader …
Read More
709-To-Lower-Case
https://leetcode.com/problems/to-lower-case
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def toLowerCase(self, str: str) -> str:
return ''.join(chr(ord(c) + 32) if 'A' <= c <= 'Z' else c for c in str)
new Solution().toLowerCase()
Score: 5
Read More
71-Simplify-Path
https://leetcode.com/problems/simplify-path
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def simplifyPath(self, path: str) -> str:
stack = []
for str in path.split('/'):
if str in ('', '.'):
continue
if str == '..':
if stack:
stack.pop()
else:
stack.append(str)
return '/' + '/'.join(stack …
Read More
710-Random-Pick-With-Blacklist
https://leetcode.com/problems/random-pick-with-blacklist
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def __init__(self, N: int, blacklist: List[int]):
self.validRange = N - len(blacklist)
self.dict = {}
for b in blacklist:
self.dict[b] = -1
for b in blacklist:
if b …
Read More
711-Number-Of-Distinct-Islands-Ii
https://leetcode.com/problems/number-of-distinct-islands-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numDistinctIslands2(self, grid: List[List[int]]) -> int:
seen = set()
def dfs(i: int, j: int):
if i < 0 or i == len(grid) or j < 0 or j == len …
Read More