C# Largest String
static string LargestString(string s)
{
var chars = s.ToCharArray();
var len = chars.Length;
var cur = "";
for (int i = len - 1; i >= 0; i--)
{
cur = chars[i] + cur;
if (cur.Length == 3)
{
if (cur == "abb")
{
chars[i] = 'b';
chars[i + 1] = 'a';
chars[i + 2] = 'a';
if (i + 4 < len && chars[i + 4] == 'b')
{
i += 4 + 1;
}
else if (i + 3 < len && chars[i + 3] == 'b')
{
i += 3 + 1;
}
else if (chars[i + 2] == 'b')
{
i += 2 + 1;
}
}
if (chars[i + 1] == 'b')
{
i += 1 + 1;
}
else
{
i++;
}
cur = "";
}
}
return new string(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.
C# Max Counters
static int[] MaxCounters(int n, int[] a)
{
var counters = new int[n];
var maxCounter = 0;
var lastUpdate = 0;
var condition = n + 1;
foreach (var v in a)
{
if (v <= n)
{
var index = v - 1;
if (counters[index] < lastUpdate)
{
counters[index] = lastUpdate;
}
counters[index]++;
maxCounter = Math.Max(counters[index], maxCounter);
}
if (v == condition)
{
lastUpdate = maxCounter;
}
}
// apply all max operations to avoid O(M*N) complexity
for (int k = 0; k < counters.Length; k++)
{
if (counters[k] < lastUpdate)
{
counters[k] = lastUpdate;
}
}
return counters;
}
This delays the expensive “set all counters to max” work until it is really needed, which keeps the solution fast.
C# Max Double Slice Sum
static long MaxDoubleSliceSum(int[] a)
{
var size = a.Length;
if (size < 3)
{
return 0;
}
var p1 = new long[size];
var p2 = new long[size];
p1[1] = 0;
p2[size - 2] = 0;
for (int i = 2; i < size - 1; i++)
{
p1[i] = Math.Max(0, p1[i - 1] + a[i - 1]);
p2[size - i - 1] = Math.Max(0, p2[size - i] + a[size - i]);
}
var sum = p1[1] + p2[1];
for (int i = 1; i < size - 1; i++)
{
sum = Math.Max(sum, p1[i] + p2[i]);
}
return sum;
}
This keeps the best sum ending on the left and starting on the right, then combines them around each middle position.
C# Max Product Of Three
static long MaxProductOfThree(int[] a)
{
var sorted = (int[])a.Clone();
Array.Sort(sorted);
var c = sorted.Length;
return Math.Max(
(long)sorted[c - 1] * sorted[c - 2] * sorted[c - 3],
(long)sorted[0] * sorted[1] * sorted[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.
C# Max Profit
static int MaxProfit(int[] a)
{
var price = a[0];
var profit = 0;
foreach (var v in a)
{
price = Math.Min(price, v);
profit = Math.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.
C# Max Slice Sum
static long MaxSliceSum(int[] a)
{
long tmp = long.MinValue;
long max = long.MinValue;
foreach (var v in a)
{
tmp = Math.Max(tmp + v, v);
max = Math.Max(max, tmp);
}
return max;
}
This is a Kadane-style scan: keep the best running sum and the best overall sum while moving once through the array.
C# Min Avg Two Slice
static int MinAvgTwoSlice(int[] a)
{
var idx = 0;
double min = (a[0] + a[1]) / 2.0;
for (int i = 0; i < a.Length - 1; i++)
{
double cur = (a[i] + a[i + 1]) / 2.0;
if (i + 2 < a.Length)
{
double three = (a[i] + a[i + 1] + a[i + 2]) / 3.0;
cur = cur < three ? cur : three;
}
if (cur < min)
{
min = cur;
idx = i;
}
}
return idx;
}
This leans on the key trick for this problem: the minimum average slice is always length 2 or 3.
C# Min Perimeter Rectangle
static long MinPerimeterRectangle(long n)
{
long i = 1;
long min = long.MaxValue;
while (i * i < n)
{
if (n % i == 0)
{
min = Math.Min(min, 2 * (i + n / i));
}
i++;
}
return min;
}
This searches factor pairs up to the square root and picks the pair with the smallest perimeter.
C# Missing Integer
static int MissingInteger(int[] a)
{
var min = 1;
var distinct = new HashSet<int>(a).ToList();
distinct.Sort();
foreach (var v in distinct)
{
if (v > 0)
{
if (min != v)
{
break;
}
min++;
}
}
return min;
}
This records the positive numbers that exist, then returns the smallest positive value that is still missing.
C# Nesting
static int Nesting(string s)
{
if (string.IsNullOrEmpty(s))
{
return 1;
}
var stack = new Stack<char>();
foreach (var v in s)
{
if (v == ')')
{
if (stack.Count == 0 || stack.Pop() != '(')
{
return 0;
}
}
else
{
stack.Push(v);
}
}
return stack.Count == 0 ? 1 : 0;
}
This treats the string like a balance counter: open parentheses add one, closing ones remove one.