Problem overview
The method minValue in class Solution takes three integers, A, B and C, and returns the smallest of them. The task forbids built-ins 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 best value seen so far
One way is to ask of each number, "is this one less than or equal to both others?", and return the first that passes. It works, but the conditions grow with every extra value.
A more scalable idea is to carry a running answer. Start by assuming A is the minimum. Look at B: if it is smaller than the current answer, it replaces it. Look at C and do the same. After the last check, the variable holds the smallest value, because it was compared with every other number along the way. Adding a fourth number would mean adding one more if, and the same pattern later becomes a loop over an array.
Step by step
- Declare
minValwithvarand set it toA. Avarcan be reassigned, and the next steps will do exactly that. Aletconstant could not be. - Write an
ifthat testsB < minVal. Swift needs no parentheses around the condition, but the braces are required. - Assign
BtominValinside it. The test is strict: a value equal to the current minimum changes nothing, so no assignment is needed. - Add a second, separate
ifforC. It must not be anelseof the first one.Chas to be compared whether or notBtook over, sinceCmay be smaller than both. - Return
minVal. After both checks it holds the smallest of the three.
The solution in Swift
class Solution {
func minValue(_ A: Int, _ B: Int, _ C: Int) -> Int {
var minVal = A
if B < minVal {
minVal = B
}
if C < minVal {
minVal = C
}
return minVal
}
}
Swift details worth knowing:
- The underscores. Each parameter is written
_ A: Int. The underscore removes the argument label, so a call readsminValue(7, 4, 9). Without it, the call would have to beminValue(A: 7, B: 4, C: 9). - Type inference.
var minVal = Aneeds no type:Ais anInt, sominValis too. - Parameters are constants. Inside the method,
A,BandCcannot be assigned to. That is why the code copiesAinto a new variable instead of overwriting it. - Conditions must be
Bool.if Balone, or a number used as a condition, is a compile error; Swift does not treat 0 as false.
Calling it:
let solution = Solution()
print(solution.minValue(7, 4, 9))
print(solution.minValue(3, 8, 3))
print(solution.minValue(-1, -5, -3))
4
3
-5
Complexity
Time: O(1). The method always does two comparisons, no matter what the values are. Space: O(1). It uses a single extra variable.
Edge cases and common mistakes
- Equal values.
minValue(2, 2, 2)returns 2. Since the comparisons are strict, no assignment happens, and the starting value is already right. - Starting from a made-up value. Initialising
minValto 0 instead ofAwould return 0 for(4, 5, 6), a number that is not among the inputs. Starting from one of the inputs avoids that. - Using
else ifforC. For(5, 3, 1),BreplacesAin the firstif, theelse ifbranch is skipped, and the method returns 3 instead of 1. - Using
letforminVal. The compiler rejects the later assignment with an error that says the value is aletconstant. - Calling the global
min. Swift has a built-inminfunction, and it would work, but this lesson asks you to write the comparison yourself.
Testing
(7, 4, 9)gives4.(3, 8, 3)gives3.(-1, -5, -3)gives-5.(2, 2, 2)gives2.(5, 3, 1)gives1.
