Problem overview
Given three integers a, b and c, return the largest. The twist is the structure: the class has two methods. maxOfTwo is a helper that returns the larger of two numbers. maxOfThree must find the largest of three by calling the helper, without comparing numbers itself, and without the built-in max.
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, because the value is the same.
Approach: split the job into a small piece and reuse it
Finding the larger of two numbers is a tiny problem. Finding the largest of three reduces to it: the largest of a, b and c is the larger of "the larger of a and b" and c. Once the helper exists, the three-number function is a single expression.
That is the point of a helper function: write the comparison once, test it on its own, and let bigger functions build on it.
Step by step
- In
maxOfTwo, returnaifa > b.returnends the function immediately, so nothing after it runs whenawins. - Otherwise return
b. The code only reaches this line whenais not greater thanb: eitherbis larger or they are equal, andbis right in both cases. Noelseis needed. - In
maxOfThree, call the helper twice, one call inside the other. The inner call,self.maxOfTwo(a, b), runs first. Its result becomes the first argument of the outer call, which compares it withc.
The solution in Python
class Solution:
def maxOfTwo(self, a: int, b: int) -> int:
if a > b:
return a
return b
def maxOfThree(self, a: int, b: int, c: int) -> int:
return self.maxOfTwo(self.maxOfTwo(a, b), c)
Details worth knowing:
self.is required. Inside a class, one method calls another through the instance, asself.maxOfTwo(...). Writing justmaxOfTwo(a, b)raises aNameError, because the method is not a plain function in scope.- Nested calls evaluate from the inside out. With
a = 3, b = 7, c = 5, the inner call returns 7, and the outer call becomesself.maxOfTwo(7, 5), which returns 7. - Parameters are local. The names
aandbinmaxOfTwoare separate from the ones inmaxOfThree. Each call gets its own copies of the values it was handed.
Tracing the nested call
Take a = 3, b = 7, c = 5. The inner call self.maxOfTwo(3, 7) returns 7. The outer call is then self.maxOfTwo(7, 5), which returns 7. The answer is 7.
Now a = -3, b = -8, c = -1. The inner call returns -3, since -3 is greater than -8. The outer call compares -3 with -1 and returns -1. In both traces the first call reduces three numbers to two, and the second reduces two to one.
The same idea extends to any number of values. For four numbers, maxOfTwo(maxOfTwo(maxOfTwo(2, 9), 4), 6) returns 9: each call folds one more value into the running winner. A loop would be the natural way to write that for a long list, but for three numbers the nested call is short and clear.
Edge cases and common mistakes
- Ties. With
a = 4, b = 4, the testa > bis false, sobis returned. It equalsa, so the answer is correct. - Negative numbers. Comparisons work the same way: -1 is greater than -3 and -8, so
maxOfThree(-3, -8, -1)returns -1. - Comparing in
maxOfThree. Adding your ownifthere defeats the exercise. The helper does all the comparing. - Ignoring the result. Calling
self.maxOfTwo(a, b)without using what it returns throws the answer away. The inner call must feed the outer one.
Testing
maxOfThree(3, 7, 5)gives7.maxOfThree(-3, -8, -1)gives-1.maxOfThree(4, 4, 2)gives4.maxOfThree(1, 2, 3)gives3, andmaxOfThree(9, 1, 1)gives9: the largest can be in any position.maxOfTwo(5, 5)gives5.
