942-Di-String-Match
https://leetcode.com/problems/di-string-match
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def diStringMatch(self, S: str) -> List[int]:
ans = []
mini = 0
maxi = len(S)
for c in S:
if c == 'I':
ans.append(mini)
mini += 1
else:
ans.append(maxi …
Read More
945-Minimum-Increment-To-Make-Array-Unique
https://leetcode.com/problems/minimum-increment-to-make-array-unique
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minIncrementForUnique(self, A: List[int]) -> int:
ans = 0
minAvailable = 0
A.sort()
for a in A:
ans += max(minAvailable - a, 0)
minAvailable = max(minAvailable, a) + 1
return ans …
Read More
946-Validate-Stack-Sequences
https://leetcode.com/problems/validate-stack-sequences
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
stack = []
i = 0 # popped's index
for x in pushed:
stack.append(x)
while stack and stack[-1] == popped …
Read More
948-Bag-Of-Tokens
https://leetcode.com/problems/bag-of-tokens
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def bagOfTokensScore(self, tokens: List[int], power: int) -> int:
ans = 0
score = 0
q = deque(sorted(tokens))
while q and (power >= q[0] or score):
while q and power …
Read More
949-Largest-Time-For-Given-Digits
https://leetcode.com/problems/largest-time-for-given-digits
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def largestTimeFromDigits(self, A: List[int]) -> str:
for time in itertools.permutations(sorted(A, reverse=True)):
if time[:2] < (2, 4) and time[2] < 6:
return '%d%d:%d …
Read More
95-Unique-Binary-Search-Trees-Ii
https://leetcode.com/problems/unique-binary-search-trees-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def generateTrees(self, n: int) -> List[TreeNode]:
if n == 0:
return []
def generateTrees(mini: int, maxi: int) -> List[Optional[int]]:
if mini > maxi:
return [None]
ans = []
for i in …
Read More
950-Reveal-Cards-In-Increasing-Order
https://leetcode.com/problems/reveal-cards-in-increasing-order
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:
q = deque()
for card in reversed(sorted(deck)):
q.rotate()
q.appendleft(card)
return list(q)
new Solution().deckRevealedIncreasing()
Score: 5 …
Read More
951-Flip-Equivalent-Binary-Trees
https://leetcode.com/problems/flip-equivalent-binary-trees
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def flipEquiv(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool:
if not root1:
return not root2
if not root2:
return not root1
if root1.val != root2.val:
return False …
Read More
952-Largest-Component-Size-By-Common-Factor
https://leetcode.com/problems/largest-component-size-by-common-factor
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class UnionFind:
def __init__(self, n: int):
self.id = [i for i in range(n + 1)]
def union(self, u: int, v: int) -> bool:
i = self.find(u)
j = self.find …
Read More
953-Verifying-An-Alien-Dictionary
https://leetcode.com/problems/verifying-an-alien-dictionary
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isAlienSorted(self, words: List[str], order: str) -> bool:
dict = {c: i for i, c in enumerate(order)}
words = [[dict[c] for c in word] for word in words …
Read More