Problem overview
The method MinValue receives three integers, A, B and C, and returns the smallest of them. The statement forbids built-in helpers such as Math.Min, so the comparison has to be written by hand.
For A = 7, B = 4, C = 9 the answer is 4. For A = 3, B = 8, C = 3 the answer is 3: when the smallest value appears twice, you simply return that value.
Approach
One way is to ask, for each number, "is it less than or equal to both of the others?" and return the first one that says yes. That works, but every extra number makes the conditions longer.
The lesson uses a simpler idea that scales to any count of values. Keep a variable, minVal, holding the smallest value seen so far. Start it at A, since after looking at one number that number is the minimum. Then look at each remaining number once, and replace minVal whenever you meet something smaller.
On 7, 4, 9: minVal starts at 7. Next, 4 is less than 7, so minVal becomes 4. Then 9 is not less than 4, so nothing changes. The method returns 4.
Step by step
- Declare
minValas anintand set it toA. This is the "best so far" after looking at the first number. - Write an
ifthat tests whetherBis less thanminVal. The body can stay empty for the moment. - Assign
BtominValinside thatif. Use a strict<: an equal value changes nothing, so there is no reason to replace it. - Add a second, separate
ifforC. It comparesCwith the currentminVal, which may already beB. If it were anelse if,Cwould be skipped wheneverBwon, and the answer could be wrong. - Return
minVal. After both checks it holds the smallest of the three.
The solution in C#
class Solution {
public int MinValue(int A, int B, int C) {
int minVal = A;
if (B < minVal) {
minVal = B;
}
if (C < minVal) {
minVal = C;
}
return minVal;
}
}
A few C# details:
- The type is declared.
int minVal = A;states the type up front. C# is statically typed, sominValcan only ever hold anint. - The condition needs parentheses and must be a
bool.if (B < minVal)is valid. C# does not treat numbers as true or false the way some languages do. - Parameters are copies. Assigning to
minValnever changesA,BorC. Value types such asintare passed by value. - Every path returns. The method ends with
return minVal;outside theifblocks, so the compiler is satisfied that all paths return anint.
The library call the statement rules out is Math.Min(a, b), which takes two values. Taking the minimum of three would need two calls, Math.Min(A, Math.Min(B, C)). Writing the comparisons yourself shows what that call does.
Edge cases and common mistakes
- Ties. With
3, 8, 3,minValstarts at 3, 8 is not smaller, and 3 is not strictly smaller than 3, so the result stays 3. Using<=would give the same answer here. - Negative numbers.
-5, -5, -9returns -9. The comparisons work the same for negatives. - Extreme values.
int.MinValueasAis never replaced, because nothing is smaller. Comparisons never overflow; only arithmetic does, and this method does none. - Using
else ifforC. For5, 3, 1,BreplacesminVal, and anelse ifwould never testC, returning 3 instead of 1. - Starting
minValat 0. If all three numbers are positive, 0 would stay the "minimum" and the answer would be wrong. Start from one of the actual inputs. - Using
=instead of<.if (minVal = B)does not compile in C#, because an assignment to anintis not abool. That is one mistake the compiler catches for you.
Testing
7, 4, 9gives4.3, 8, 3gives3.-5, -5, -9gives-9.int.MinValue, 0, 1gives-2147483648.