75-Sort-Colors
https://leetcode.com/problems/sort-colors
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def sortColors(self, nums: List[int]) -> None:
zero = -1
one = -1
two = -1
for num in nums:
if num == 0:
two += 1
one += 1
zero += 1
nums[two] = 2 …
Read More
751-Ip-To-Cidr
https://leetcode.com/problems/ip-to-cidr
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def ipToCIDR(self, ip: str, n: int) -> List[str]:
ans = []
num = self._getNum(ip.split('.'))
while n > 0:
lowbit = num & -num
count = self._maxLow(n) if lowbit == 0 else …
Read More
753-Cracking-The-Safe
https://leetcode.com/problems/cracking-the-safe
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def crackSafe(self, n: int, k: int) -> str:
passwordSize = k**n
path = '0' * n
seen = set()
seen.add(path)
def dfs(path: str) -> str:
if len(seen) == passwordSize:
return …
Read More
754-Reach-A-Number
https://leetcode.com/problems/reach-a-number
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def reachNumber(self, target: int) -> int:
ans = 0
pos = 0
target = abs(target)
while pos < target:
ans += 1
pos += ans
while (pos - target) & 1:
ans += 1
pos += ans
return …
Read More
756-Pyramid-Transition-Matrix
https://leetcode.com/problems/pyramid-transition-matrix
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def pyramidTransition(self, bottom: str, allowed: List[str]) -> bool:
prefixToBlocks = defaultdict(list)
for a in allowed:
prefixToBlocks[a[:2]].append(a[2])
def dfs(row: str, nextRow: str, i …
Read More
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