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
954-Array-Of-Doubled-Pairs
https://leetcode.com/problems/array-of-doubled-pairs
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
count = Counter(A)
for key in sorted(count, key=abs):
if count[key] > count[2 * key]:
return False
count[2 * key] -= count …
Read More
957-Prison-Cells-After-N-Days
https://leetcode.com/problems/prison-cells-after-n-days
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def prisonAfterNDays(self, cells: List[int], N: int) -> List[int]:
nextDayCells = [0] * len(cells)
day = 0
while N > 0:
N -= 1
for i in range(1, len(cells) - 1 …
Read More
96-Unique-Binary-Search-Trees
https://leetcode.com/problems/unique-binary-search-trees
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numTrees(self, n: int) -> int:
# G[i] := # Of unique BST's that store values 1..i
G = [1, 1] + [0] * (n - 1)
for i in range(2, n …
Read More