Python Largest String
def largest_string(s: str) -> str:
chars = list(s)
length = len(chars)
# instead of raising; this helper preserves that read behavior so the
# lookahead checks below can't index past the end of the list.
def char_at(idx: int) -> str:
return chars[idx] if 0 <= idx < length else ""
cur = ""
i = length - 1
while i >= 0:
cur = char_at(i) + cur
if len(cur) == 3:
if cur == "abb":
chars[i] = "b"
chars[i + 1] = "a"
chars[i + 2] = "a"
if char_at(i + 4) == "b":
i += 4 + 1
elif char_at(i + 3) == "b":
i += 3 + 1
elif chars[i + 2] == "b":
i += 2 + 1
if char_at(i + 1) == "b":
i += 1 + 1
else:
i += 1
cur = ""
i -= 1
return "".join(chars)
This builds the biggest valid string it can under the challenge rules by always choosing the best next character it is allowed to use.
Python Max Counters
def max_counters(n: int, a: list[int]) -> list[int]:
counters = [0] * n
max_counter = 0
last_update = 0
condition = n + 1
for v in a:
if v <= n:
index = v - 1
if counters[index] < last_update:
counters[index] = last_update
counters[index] += 1
max_counter = max(counters[index], max_counter)
if v == condition:
last_update = max_counter
for k, v in enumerate(counters):
if v < last_update:
counters[k] = last_update
return counters
This delays the expensive “set all counters to max” work until it is really needed, which keeps the solution fast.
Python Max Double Slice Sum
def max_double_slice_sum(a: list[int]) -> int:
size = len(a)
if size < 3:
return 0
p1 = {1: 0}
p2 = {size - 2: 0}
for i in range(2, size - 1):
p1[i] = max(0, p1[i - 1] + a[i - 1])
p2[size - i - 1] = max(0, p2[size - i] + a[size - i])
total = p1[1] + p2[1]
for i in range(1, size - 1):
total = max(total, p1[i] + p2[i])
return total
This keeps the best sum ending on the left and starting on the right, then combines them around each middle position.
Python Max Product Of Three
def max_product_of_three(a: list[int]) -> int:
a = sorted(a)
c = len(a)
return max(a[c - 1] * a[c - 2] * a[c - 3], a[0] * a[1] * a[c - 1])
This checks the useful extremes, because the best product can come from either the three largest numbers or two negatives plus one large positive.
Python Max Profit
def max_profit(a: list[int]) -> int:
price = a[0]
profit = 0
for v in a:
price = min(price, v)
profit = max(profit, v - price)
return profit
This tracks the lowest buy price seen so far and updates the best profit as it scans the prices once.
Python Max Slice Sum
def max_slice_sum(a: list[int]) -> int:
tmp = max_sum = float("-inf")
for v in a:
tmp = max(tmp + v, v)
max_sum = max(max_sum, tmp)
return int(max_sum)
This is a Kadane-style scan: keep the best running sum and the best overall sum while moving once through the array.
Python Min Avg Two Slice
def min_avg_two_slice(a: list[int]) -> int:
idx = 0
min_avg = (a[0] + a[1]) / 2
for i in range(len(a) - 1):
cur = (a[i] + a[i + 1]) / 2
if i + 2 < len(a):
three = (a[i] + a[i + 1] + a[i + 2]) / 3
cur = min(cur, three)
if cur < min_avg:
min_avg = cur
idx = i
return idx
This leans on the key trick for this problem: the minimum average slice is always length 2 or 3.
Python Min Perimeter Rectangle
def min_perimeter_rectangle(n: int) -> int:
i = 1
min_perimeter = float("inf")
while i * i < n:
if n % i == 0:
min_perimeter = min(min_perimeter, 2 * (i + n // i))
i += 1
return int(min_perimeter)
This searches factor pairs up to the square root and picks the pair with the smallest perimeter.
Python Missing Integer
def missing_integer(a: list[int]) -> int:
min_val = 1
for v in sorted(set(a)):
if v > 0:
if min_val != v:
break
min_val += 1
return min_val
This records the positive numbers that exist, then returns the smallest positive value that is still missing.
Python Nesting
def nesting(s: str) -> int:
if not s:
return 1
stack: list[str] = []
for ch in s:
if ch == ")":
if not stack or stack.pop() != "(":
return 0
elif ch:
stack.append(ch)
return 1 if not stack else 0
This treats the string like a balance counter: open parentheses add one, closing ones remove one.