889-Construct-Binary-Tree-From-Preorder-And-Postorder-Traversal
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def constructFromPrePost(self, pre: List[int], post: List[int]) -> Optional[TreeNode]:
postToIndex = {num: i for i, num in enumerate(post)}
def build(preStart: int, preEnd: int, postStart: int, postEnd …
Read More
89-Gray-Code
https://leetcode.com/problems/gray-code
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def grayCode(self, n: int) -> List[int]:
ans = [0]
for i in range(n):
for j in reversed(range(len(ans))):
ans.append(ans[j] | 1 << i)
return ans …
Read More
890-Find-And-Replace-Pattern
https://leetcode.com/problems/find-and-replace-pattern
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
def isIsomorphic(w: str, p: str) -> bool:
return [*map(w.index, w)] == [*map(p.index, p)]
return [word for …
Read More
891-Sum-Of-Subsequence-Widths
https://leetcode.com/problems/sum-of-subsequence-widths
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def sumSubseqWidths(self, nums: List[int]) -> int:
kMod = 1_000_000_007
n = len(nums)
ans = 0
exp = 1
nums.sort()
for i in range(n):
ans += (nums[i] - nums[n …
Read More
892-Surface-Area-Of-3D-Shapes
https://leetcode.com/problems/surface-area-of-3d-shapes
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def surfaceArea(self, grid: List[List[int]]) -> int:
ans = 0
for i in range(len(grid)):
for j in range(len(grid)):
if grid[i][j]:
ans += grid[i …
Read More
893-Groups-Of-Special-Equivalent-Strings
https://leetcode.com/problems/groups-of-special-equivalent-strings
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numSpecialEquivGroups(self, A: List[str]) -> int:
return len({''.join(sorted(s[::2])) + ''.join(sorted(s[1::2])) for s in A})
new Solution().numSpecialEquivGroups()
Score: 5
Read More
894-All-Possible-Full-Binary-Trees
https://leetcode.com/problems/all-possible-full-binary-trees
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
@functools.lru_cache(None)
def allPossibleFBT(self, n: int) -> List[Optional[TreeNode]]:
if n % 2 == 0:
return []
if n == 1:
return [TreeNode(0)]
ans = []
for leftCount in range(n):
rightCount …
Read More
896-Monotonic-Array
https://leetcode.com/problems/monotonic-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isMonotonic(self, A: List[int]) -> bool:
increasing = True
decreasing = True
for i in range(1, len(A)):
increasing &= A[i - 1] <= A[i]
decreasing &= A[i - 1] >= A …
Read More
897-Increasing-Order-Search-Tree
https://leetcode.com/problems/increasing-order-search-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def increasingBST(self, root: TreeNode, tail: TreeNode = None) -> TreeNode:
if not root:
return tail
res = self.increasingBST(root.left, root)
root.left = None
root.right = self.increasingBST(root.right …
Read More
899-Orderly-Queue
https://leetcode.com/problems/orderly-queue
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def orderlyQueue(self, S: str, K: int) -> str:
return ''.join(sorted(S)) if K > 1 else min(S[i:] + S[:i] for i in range(len(S)))
Read More