593-Valid-Square
https://leetcode.com/problems/valid-square
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def validSquare(self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]) -> bool:
def dist(p1: List[int], p2: List[int]) -> int:
return (p1[0] - p2 …
Read More
594-Longest-Harmonious-Subsequence
https://leetcode.com/problems/longest-harmonious-subsequence
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findLHS(self, nums: List[int]) -> int:
ans = 0
count = Counter(nums)
for num, freq in count.items():
if num + 1 in count:
ans = max(ans, freq + count[num …
Read More
598-Range-Addition-Ii
https://leetcode.com/problems/range-addition-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int:
minY = m
minX = n
for y, x in ops:
minY = min(minY, y)
minX = min(minX, x …
Read More
599-Minimum-Index-Sum-Of-Two-Lists
https://leetcode.com/problems/minimum-index-sum-of-two-lists
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
ans = []
restaurantToIndex = {restaurant: i for i,
restaurant in enumerate(list1)}
minSum = math.inf
for i, restaurant in enumerate …
Read More
6-Holoviews-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 holoviews | grep "Version:"
import holoviews as hv
hv.extension('bokeh')
hv.Curve …
Read More
6-Zigzag-Conversion
https://leetcode.com/problems/zigzag-conversion
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def convert(self, s: str, numRows: int) -> str:
rows = [''] * numRows
k = 0
direction = (numRows == 1) - 1
for c in s:
rows[k] += c
if k == 0 or k == numRows …
Read More
60-Permutation-Sequence
https://leetcode.com/problems/permutation-sequence
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def getPermutation(self, n: int, k: int) -> str:
ans = ''
nums = [i + 1 for i in range(n)]
factorial = [1] * (n + 1) # factorial[i] := i!
for i in range(2 …
Read More
605-Can-Place-Flowers
https://leetcode.com/problems/can-place-flowers
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
for i, flower in enumerate(flowerbed):
if flower == 0 and (i == 0 or flowerbed[i - 1] == 0) and (i == len …
Read More
606-Construct-String-From-Binary-Tree
https://leetcode.com/problems/construct-string-from-binary-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def tree2str(self, t: Optional[TreeNode]) -> str:
def dfs(root: Optional[TreeNode]) -> str:
if not root:
return ''
if root.right:
return str(root.val) + '(' + dfs(root.left) + ')(' + dfs(root …
Read More
609-Find-Duplicate-File-In-System
https://leetcode.com/problems/find-duplicate-file-in-system
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
contentToPathFiles = defaultdict(list)
for path in paths:
words = path.split(' ')
rootPath = words[0] # "root/d1/d2/.../dm"
for fileAndContent in …
Read More