758-Bold-Words-In-String
https://leetcode.com/problems/bold-words-in-string
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def boldWords(self, words: List[str], s: str) -> str:
n = len(s)
ans = []
# bold[i] := True if s[i] should be bolded
bold = [0] * n
boldEnd = -1 # s[i …
Read More
759-Employee-Free-Time
https://leetcode.com/problems/employee-free-time
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def employeeFreeTime(self, schedule: '[[Interval]]') -> '[Interval]':
ans = []
intervals = []
for s in schedule:
intervals.extend(s)
intervals.sort(key=lambda x: x.start)
prevEnd = intervals[0].end
for interval in …
Read More
76-Minimum-Window-Substring
https://leetcode.com/problems/minimum-window-substring
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minWindow(self, s: str, t: str) -> str:
count = Counter(t)
required = len(t)
bestLeft = -1
minLength = len(s) + 1
l = 0
for r, c in enumerate(s):
count …
Read More
761-Special-Binary-String
https://leetcode.com/problems/special-binary-string
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def makeLargestSpecial(self, S: str) -> str:
specials = []
count = 0
i = 0
for j, c in enumerate(S):
count += 1 if c == '1' else -1
if count == 0:
specials.append …
Read More
763-Partition-Labels
https://leetcode.com/problems/partition-labels
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def partitionLabels(self, S: str) -> List[int]:
ans = []
letterToRightmostIndex = {c: i for i, c in enumerate(S)}
l = 0
r = 0
for i, c in enumerate(S):
r = max …
Read More
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