Problem overview
The method minValue(int A, int B, int C) takes three integers and returns the smallest. The statement forbids built-in helpers such as min, so the comparison has to be written by hand with if.
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, returning either copy gives the same number.
Approach: keep the smallest value seen so far
There are two natural ways to do this. The first checks each candidate against the others: A is the minimum if A <= B && A <= C; if not, check B the same way; if neither, it must be C. That works, but each condition names every other variable, so with four or five numbers it becomes long and easy to get wrong.
The lesson uses the second way, a running minimum. Start by assuming A is the smallest, then look at the other values one at a time. When one is smaller than the current minimum, it replaces it. After the last value has been checked, the variable holds the answer. Adding a fourth number would need only one more if, which is why this form scales.
For comparison, here is the first way written out. It is also correct, and it returns on the first condition that holds:
if (A <= B && A <= C) {
return A;
}
if (B <= A && B <= C) {
return B;
}
return C;
The conditions use <= so that a tie still counts as the minimum. With a strict <, the input (5, 5, 5) would fail both tests and be rescued only by the final return C. The running minimum has no such subtlety, because it never has to decide ties: an equal value simply changes nothing.
Step by step
- Declare
minValas anintand set it toA. Until a smaller value shows up,Ais the best answer so far. - Write an
ifcomparingBwithminVal:B < minVal. - Inside it, set
minVal = B. Only a strictly smaller value replaces the minimum. IfBequalsminVal, nothing changes, and nothing needs to. - Write a second, separate
ifforC. It checksC < minValand, if true, setsminVal = C. It must be a separateif, not anelse, soCis compared whether or notBreplaced the minimum. - Return
minVal. Every value has been compared, so it is 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;
}
};
Walk through minValue(7, 4, 9). minVal starts at 7. Since 4 < 7, it becomes 4. Since 9 < 4 is false, it stays 4, and the method returns 4.
The same code on (9, 8, 7) shows why the second check is separate: minVal goes 9, then 8 after B, then 7 after C, because each comparison uses the latest minimum.
If you may use the standard library elsewhere, std::min({A, B, C}) from <algorithm> does the same in one line. This lesson asks for the manual version to practice conditions.
The same idea extends to any number of values. For a vector<int> named v that is not empty, start with the first element and loop over the rest:
int minVal = v[0];
for (size_t i = 1; i < v.size(); i++) {
if (v[i] < minVal) {
minVal = v[i];
}
}
This is the pattern behind finding a smallest element in every later chapter. Starting from the first element, not from 0 or a made-up large number, keeps it correct for any input, as long as there is at least one element.
Each if can also be written with the conditional operator, minVal = (B < minVal) ? B : minVal;. It means the same thing: pick B when it is smaller, otherwise keep what is there. Beginners usually find the plain if easier to read and to step through, and behaves the same. Whichever you choose, say the logic out loud as "if this value is smaller than the best so far, it becomes the best so far". If that sentence matches your code, the code is right.
Complexity
Time: O(1). There are always two comparisons, whatever the values are.
Space: O(1). The method uses one extra int.
Edge cases and common mistakes
- Equal values.
(5, 5, 5)returns 5. Neither comparison is true, sominValstays atA. - Negative numbers.
(-1, -5, -3)returns-5. The comparisons treat negatives correctly:-5 < -1. - Extreme values.
(INT_MIN, 0, INT_MAX)returnsINT_MIN. The method only compares, with no subtraction or addition, so there is no overflow. - Using
else iffor the second check. IfBis smaller thanAand the check forCis skipped, the answer is wrong for(9, 8, 7), whereCis the smallest. - Using
=instead of==or<.if (B = minVal)assigns instead of comparing. It compiles, and the condition is true whenever the value is not 0. - Starting
minValat 0. If all three numbers are positive, 0 would stay the minimum, which is wrong. Start from one of the inputs.
Testing
(7, 4, 9)gives4.(3, 8, 3)gives3.(9, 8, 7)gives7.(-1, -5, -3)gives-5.(5, 5, 5)gives5.
