Problem overview
The class has two methods. maxOfTwo is a helper: it receives two integers and returns the larger one. maxOfThree receives three integers, a, b and c, and returns the largest, using the helper for every comparison. Swift's built-in max is not used.
For a = 3, b = 7, c = 5 the answer is 7. For a = -3, b = -8, c = -1 it is -1, the negative number closest to zero. For a = 4, b = 4, c = 2 it is 4: when two numbers tie, either one is the answer, since the value is the same.
Approach
Finding the largest of three numbers is two steps of finding the larger of two. The larger of a and b is a candidate; the larger of that candidate and c is the winner. Writing the comparison once, in maxOfTwo, and calling it twice is the point of a helper function: the logic lives in one place, and maxOfThree reads as a plan rather than a pile of if statements.
The two calls nest. The inner one, maxOfTwo(a, b), runs first, and its result becomes the first argument of the outer call.
Tracing one call
Take maxOfThree(3, 7, 5). The inner call maxOfTwo(3, 7) compares 3 with 7, finds that 3 is not greater, and returns 7. The expression becomes maxOfTwo(7, 5), where 7 is greater than 5, so the final answer is 7. For maxOfThree(-3, -8, -1) the inner call returns -3, and the outer call then compares -3 with -1 and returns -1.
The same idea extends to any number of values. Four numbers would be maxOfTwo(maxOfThree(a, b, c), d): each new function builds on the one before it, and no comparison is ever written twice.
Step by step
- In
maxOfTwo, returnawhena > b.returnends the function immediately, so nothing after it runs for that case. - Return
bbelow theif. The function only gets there whenais not greater thanb:bis larger, or the two are equal and either is correct. Noelseis needed. - In
maxOfThree, nest the calls. ReturnmaxOfTwo(maxOfTwo(a, b), c).
The solution in Swift
class Solution {
func maxOfTwo(_ a: Int, _ b: Int) -> Int {
if a > b {
return a
}
return b
}
func maxOfThree(_ a: Int, _ b: Int, _ c: Int) -> Int {
return maxOfTwo(maxOfTwo(a, b), c)
}
}
Swift details worth knowing:
- Calling a sibling method. Inside a class, a method calls another method of the same class by its bare name.
self.maxOfTwo(a, b)means the same thing, and Swift allows leaving outselfhere. - No labels in the calls. Each parameter is declared with a leading
_, so the arguments are passed without names. Without the underscores, the calls would have to readmaxOfTwo(a: a, b: b). - Parameters are constants.
aandbinsidemaxOfTwoare separate from theaandbofmaxOfThree. Each call receives copies of the values, and neither function can change the other's variables.
Edge cases and common mistakes
- Ties. With
a = 4andb = 4, the conditiona > bis false and the helper returnsb, which is 4. Using>=would also work. - Negative numbers. Nothing special is needed. A solution that starts from 0 as the "largest so far" would wrongly return 0 for
-3, -8, -1; comparing the inputs with each other avoids that. - The largest in any position. Because every value takes part in a comparison, the answer is found whether it is first, second or third.
- Comparing in
maxOfThree. Writingifstatements there goes against the lesson's design: all comparing happens in the helper.
Testing
maxOfThree(3, 7, 5)gives 7.maxOfThree(-3, -8, -1)gives -1.maxOfThree(4, 4, 2)gives 4.maxOfThree(9, 1, 2)gives 9, andmaxOfThree(1, 2, 9)gives 9.
