Rust Largest String
fn largest_string(s: &str) -> String {
let mut bytes: Vec<u8> = s.bytes().collect();
let len = bytes.len() as i64;
let mut cur: Vec<u8> = Vec::new();
let mut i = len - 1;
while i >= 0 {
cur.insert(0, bytes[i as usize]);
if cur.len() == 3 {
if cur == b"abb" {
bytes[i as usize] = b'b';
bytes[i as usize + 1] = b'a';
bytes[i as usize + 2] = b'a';
if bytes.get(i as usize + 4) == Some(&b'b') {
i += 4 + 1;
} else if bytes.get(i as usize + 3) == Some(&b'b') {
i += 3 + 1;
} else if bytes.get(i as usize + 2) == Some(&b'b') {
i += 2 + 1;
}
}
if bytes.get(i as usize + 1) == Some(&b'b') {
i += 1 + 1;
} else {
i += 1;
}
cur.clear();
}
i -= 1;
}
String::from_utf8(bytes).unwrap()
}
This builds the biggest valid string it can under the challenge rules by always choosing the best next character it is allowed to use.
Rust Max Counters
fn max_counters(n: i64, a: &[i64]) -> Vec<i64> {
let mut counters = vec![0i64; n as usize];
let mut max_counter = 0i64;
let mut last_update = 0i64;
let condition = n + 1;
for &v in a {
if v <= n {
let index = (v - 1) as usize;
if counters[index] < last_update {
counters[index] = last_update;
}
counters[index] += 1;
max_counter = max_counter.max(counters[index]);
}
if v == condition {
last_update = max_counter;
}
}
for c in counters.iter_mut() {
if *c < last_update {
*c = last_update;
}
}
counters
}
This delays the expensive “set all counters to max” work until it is really needed, which keeps the solution fast.
Rust Max Double Slice Sum
fn max_double_slice_sum(a: &[i64]) -> i64 {
let size = a.len();
if size < 3 {
return 0;
}
let mut p1 = vec![0i64; size];
let mut p2 = vec![0i64; size];
for i in 2..size - 1 {
p1[i] = 0.max(p1[i - 1] + a[i - 1]);
p2[size - i - 1] = 0.max(p2[size - i] + a[size - i]);
}
let mut sum = p1[1] + p2[1];
for i in 1..size - 1 {
sum = sum.max(p1[i] + p2[i]);
}
sum
}
This keeps the best sum ending on the left and starting on the right, then combines them around each middle position.
Rust Max Product Of Three
fn max_product_of_three(mut a: Vec<i64>) -> i64 {
a.sort();
let c = a.len();
(a[c - 1] * a[c - 2] * a[c - 3]).max(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.
Rust Max Profit
fn max_profit(a: &[i64]) -> i64 {
let mut price = a[0];
let mut profit = 0i64;
for &v in a {
price = price.min(v);
profit = profit.max(v - price);
}
profit
}
This tracks the lowest buy price seen so far and updates the best profit as it scans the prices once.
Rust Max Slice Sum
fn max_slice_sum(a: &[i64]) -> i64 {
let mut tmp = a[0];
let mut max = a[0];
for &v in &a[1..] {
tmp = (tmp + v).max(v);
max = max.max(tmp);
}
max
}
This is a Kadane-style scan: keep the best running sum and the best overall sum while moving once through the array.
Rust Min Avg Two Slice
fn min_avg_two_slice(a: &[i64]) -> i64 {
let mut idx = 0i64;
let mut min = (a[0] + a[1]) as f64 / 2.0;
for i in 0..a.len() - 1 {
let mut cur = (a[i] + a[i + 1]) as f64 / 2.0;
if i + 2 < a.len() {
let three = (a[i] + a[i + 1] + a[i + 2]) as f64 / 3.0;
cur = cur.min(three);
}
if cur < min {
min = cur;
idx = i as i64;
}
}
idx
}
This leans on the key trick for this problem: the minimum average slice is always length 2 or 3.
Rust Min Perimeter Rectangle
fn min_perimeter_rectangle(n: i64) -> i64 {
let mut i = 1i64;
let mut min = i64::MAX;
while i * i < n {
if n % i == 0 {
min = min.min(2 * (i + n / i));
}
i += 1;
}
min
}
This searches factor pairs up to the square root and picks the pair with the smallest perimeter.
Rust Missing Integer
use std::collections::HashSet;
fn missing_integer(a: &[i64]) -> i64 {
let mut vals: Vec<i64> = a.iter().copied().collect::<HashSet<_>>().into_iter().collect();
vals.sort();
let mut min = 1i64;
for v in vals {
if v > 0 {
if min != v {
break;
}
min += 1;
}
}
min
}
This records the positive numbers that exist, then returns the smallest positive value that is still missing.
Rust Nesting
fn nesting(s: &str) -> i64 {
if s.is_empty() {
return 1;
}
let mut stack = Vec::new();
for c in s.chars() {
if c == ')' {
if stack.pop() != Some('(') {
return 0;
}
} else {
stack.push(c);
}
}
if stack.is_empty() { 1 } else { 0 }
}
This treats the string like a balance counter: open parentheses add one, closing ones remove one.