766-Toeplitz-Matrix
https://leetcode.com/problems/toeplitz-matrix
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:
for i in range(len(matrix) - 1):
for j in range(len(matrix[0]) - 1):
if matrix[i][j] != matrix[i …
Read More
767-Reorganize-String
https://leetcode.com/problems/reorganize-string
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def reorganizeString(self, s: str) -> str:
count = Counter(s)
if max(count.values()) > (len(s) + 1) // 2:
return ''
ans = []
maxHeap = [(-freq, c) for c, freq in count.items()]
heapq …
Read More
768-Max-Chunks-To-Make-Sorted-Ii
https://leetcode.com/problems/max-chunks-to-make-sorted-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxChunksToSorted(self, arr: List[int]) -> int:
n = len(arr)
ans = 0
maxi = -math.inf
mini = [arr[-1]] * n
for i in reversed(range(n - 1)):
mini[i] = min …
Read More
769-Max-Chunks-To-Make-Sorted
https://leetcode.com/problems/max-chunks-to-make-sorted
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxChunksToSorted(self, arr: List[int]) -> int:
ans = 0
maxi = -math.inf
for i, a in enumerate(arr):
maxi = max(maxi, a)
if maxi == i:
ans += 1
return ans …
Read More
77-Combinations
https://leetcode.com/problems/combinations
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
ans = []
def dfs(s: int, path: List[int]) -> None:
if len(path) == k:
ans.append(path.copy())
return
for …
Read More
770-Basic-Calculator-Iv
https://leetcode.com/problems/basic-calculator-iv
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Poly:
def __init__(self, term: str = None, coef: int = None):
if term and coef:
self.terms = Counter({term: coef})
else:
self.terms = Counter()
def __add__(self, other):
for …
Read More
771-Jewels-And-Stones
https://leetcode.com/problems/jewels-and-stones
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numJewelsInStones(self, J: str, S: str) -> int:
jewels = set(J)
return sum(s in jewels for s in S)
new Solution().numJewelsInStones()
Score: 5
Read More
772-Basic-Calculator-Iii
https://leetcode.com/problems/basic-calculator-iii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def calculate(self, s: str) -> int:
nums = []
ops = []
def calc():
b = nums.pop()
a = nums.pop()
op = ops.pop()
if op == '+':
nums.append(a + b)
elif op == '-':
nums.append …
Read More
774-Minimize-Max-Distance-To-Gas-Station
https://leetcode.com/problems/minimize-max-distance-to-gas-station
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minmaxGasDist(self, stations: List[int], k: int) -> float:
kErr = 1e-6
l = 0
r = stations[-1] - stations[0]
# True if can use k or less gas stations to ensure …
Read More
775-Global-And-Local-Inversions
https://leetcode.com/problems/global-and-local-inversions
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isIdealPermutation(self, A: List[int]) -> bool:
for i, a in enumerate(A):
if abs(a - i) > 1:
return False
return True
new Solution().isIdealPermutation()
Score: 5
Read More