1049-Last-Stone-Weight-Ii
https://leetcode.com/problems/last-stone-weight-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def lastStoneWeightII(self, stones: List[int]) -> int:
summ = sum(stones)
s = 0
dp = [True] + [False] * summ
for stone in stones:
for w in range(summ // 2 + 1)[::-1]:
if …
Read More
105-Construct-Binary-Tree-From-Preorder-And-Inorder-Traversal
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
inToIndex = {num: i for i, num in enumerate(inorder)}
def build(preStart: int, preEnd: int, inStart: int, inEnd …
Read More
1051-Height-Checker
https://leetcode.com/problems/height-checker
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def heightChecker(self, heights: List[int]) -> int:
ans = 0
currentHeight = 1
count = [0] * 101
for height in heights:
count[height] += 1
for height in heights:
while count[currentHeight] == 0 …
Read More
1052-Grumpy-Bookstore-Owner
https://leetcode.com/problems/grumpy-bookstore-owner
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxSatisfied(self, customers: List[int], grumpy: List[int], X: int) -> int:
satisfied = sum(c for i, c in enumerate(customers) if grumpy[i] == 0)
madeSatisfied = 0
windowSatisfied = 0 …
Read More
1053-Previous-Permutation-With-One-Swap
https://leetcode.com/problems/previous-permutation-with-one-swap
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def prevPermOpt1(self, A: List[int]) -> List[int]:
n = len(A)
l = n - 2
r = n - 1
while l >= 0 and A[l] <= A[l + 1]:
l -= 1
if …
Read More
1054-Distant-Barcodes
https://leetcode.com/problems/distant-barcodes
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
ans = [0] * len(barcodes)
count = Counter(barcodes)
i = 0 # ans' index
maxNum = max(count, key=count.get)
def fillAns(num: int …
Read More
1057-Campus-Bikes
https://leetcode.com/problems/campus-bikes
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def assignBikes(self, workers: List[List[int]], bikes: List[List[int]]) -> List[int]:
ans = [-1] * len(workers)
usedBikes = [False] * len(bikes)
# buckets[k] := (i, j), where k = dist(workers …
Read More
1058-Minimize-Rounding-Error-To-Meet-Target
https://leetcode.com/problems/minimize-rounding-error-to-meet-target
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minimizeError(self, prices: List[str], target: int) -> str:
# A[i] := (costCeil - costFloor, costCeil, costFloor)
# The lower the costCeil - costFloor, the cheaper to ceil it
A = []
sumFloored = 0
sumCeiled …
Read More
106-Construct-Binary-Tree-From-Inorder-And-Postorder-Traversal
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
inToIndex = {num: i for i, num in enumerate(inorder)}
def build(inStart: int, inEnd: int, postStart: int, postEnd …
Read More
1063-Number-Of-Valid-Subarrays
https://leetcode.com/problems/number-of-valid-subarrays
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def validSubarrays(self, nums: List[int]) -> int:
# For each num in nums, each element x in the stack can be the leftmost
# Element s.t. [x, num] forms a …
Read More