692-Top-K-Frequent-Words
https://leetcode.com/problems/top-k-frequent-words
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def topKFrequent(self, words: List[str], k: int) -> List[str]:
ans = []
bucket = [[] for _ in range(len(words) + 1)]
for word, freq in Counter(words).items():
bucket[freq].append …
Read More
694-Number-Of-Distinct-Islands
https://leetcode.com/problems/number-of-distinct-islands
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numDistinctIslands(self, grid: List[List[int]]) -> int:
seen = set()
def dfs(i: int, j: int, i0: int, j0: int):
if i < 0 or i == len(grid) or j …
Read More
695-Max-Area-Of-Island
https://leetcode.com/problems/max-area-of-island
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
def dfs(i: int, j: int) -> int:
if i < 0 or i == len(grid) or j < 0 or j == len(grid …
Read More
696-Count-Binary-Substrings
https://leetcode.com/problems/count-binary-substrings
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def countBinarySubstrings(self, s: str) -> int:
ans = 0
prevCount = 0
equals = 1
for i in range(len(s) - 1):
if s[i] == s[i + 1]:
equals += 1
else:
ans …
Read More
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-Dash-1
'conda env: ml312-2024; pyv: 3.12.7 | packaged by Anaconda, Inc. | (main, Oct 4 2024, 13:27:36) [GCC 11.2.0]'
!pip show dash | grep "Version:"
from dash import Dash, dcc, html
import plotly.express as px
app …
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