Problem overview
The method minValue receives three integers A, B and C and returns the smallest one. The statement forbids built-in helpers such as 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, returning either copy gives the same number.
Approach: keep the smallest value seen so far
One way is to test each number as a candidate: A is the minimum if A <= B and A <= C, B is the minimum if B <= A and B <= C, and if neither holds, it must be C. That works, but it writes out every pairwise comparison, and the number of conditions grows quickly with more values.
The lesson uses a scalable idea instead. Keep a variable minVal that holds the smallest number you have looked at so far. Start with A, since after looking at one number it is also the smallest. Then look at each remaining number once: if it is smaller than minVal, replace minVal. When all numbers have been seen, minVal is the answer. For three values it takes two comparisons, and for a million values it would take 999,999: one per extra value, always.
Here is A = 7, B = 4, C = 9. Start with minVal = 7. Is 4 less than 7? Yes, so minVal becomes 4. Is 9 less than 4? No, so it stays 4. The method returns 4.
Step by step
- Start with
A. Declare anintnamedminValand initialize it withA. Until something smaller appears,Ais the answer. - Compare
B. Anifchecks whetherBis less thanminVal. - Update inside the
if. AssignBtominVal. Only a strictly smaller value replaces the current minimum, so equal values change nothing, and that is harmless. - Compare
Cwith a second, separateif. It must not be anelse if.Chas to be compared with the current minimum whether or notBreplacedA. Withelse if, the input7, 4, 2would stop afterBand return 4. - Return
minVal. After both checks it holds the smallest of the three.
The solution in Java
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;
}
}
The Java details worth knowing:
- The variable is reassigned, not redeclared. Inside the
ifblocks,minVal = B;has no type in front. Writingint minVal = B;there does not compile, becauseminValis already declared in the enclosing scope and Java does not allow a second local variable with the same name. - Parameter names.
A,BandCare valid Java names, though by convention parameters start with a lowercase letter. The names here match the lesson. - The library way. Outside this exercise, the usual one-liner is
Math.min(a, Math.min(b, c)), becauseMath.mintakes exactly two arguments. The exercise asks you to do by hand what that call does. - A conditional expression can shorten each step:
minVal = (B < minVal) ? B : minVal;. Theifform is easier to read and extend.
Complexity
Time: O(1) for three values. The method makes exactly two comparisons whatever the numbers are. If the same idea were applied to n values in an array, it would be O(n): one comparison per element.
Space: O(1). The method uses one extra variable, minVal.
Edge cases and common mistakes
- Equal values.
3, 8, 3returns 3, and5, 5, 5returns 5. Using<or<=gives the same number, because swapping a value for an equal one changes nothing. - Negative numbers.
-1, -5, -3returns-5. The comparison works on the numbers' real values, so -5 is smaller than -1. - Extreme values.
Integer.MIN_VALUEandInteger.MAX_VALUEwork, because comparing twointvalues never overflows. Computing a difference, such asA - B < 0, can overflow and give a wrong answer, so compare directly. - Using
else iffor the second check, as described in step 4. - Starting
minValat0. For all-positive inputs the result would be 0, which is not one of the numbers. Always start from one of the actual values. - Returning the position instead of the value. The method returns the number itself, not which parameter held it.
Testing
7, 4, 9gives4.3, 8, 3gives3.1, 2, 3gives1, and3, 2, 1gives1.-1, -5, -3gives-5.5, 5, 5gives5.Integer.MIN_VALUE, 0, Integer.MAX_VALUEgives-2147483648.
