Maximum Product of Three Numbers
Real-world version: Largest Triple DriftThe statement here is our own: the same inputs, outputs and technique.
A calibration rig logs how far a gauge drifted each shift. drifts[i] is the signed drift for shift i, negative when the gauge fell short and positive when it overshot.
The rig's stress score for three shifts is the product of their three drift values. Pick three shifts at three different positions and return the largest stress score reachable.
Note that two badly negative drifts multiply into a large positive number, so the three biggest values are not always the winning pick.
Examples
Example 1
- Input
- drifts = [3, 5, 1, 8]
- Output
- 120
Every value is positive, so the three largest, 3, 5 and 8, give the best score of 120.
Example 2
- Input
- drifts = [-9, -8, 1, 2]
- Output
- 144
Taking -9 and -8 together cancels both signs, and pairing them with 2 beats the 1 * 2 * -8 available from the three largest values.
Example 3
- Input
- drifts = [-5, -4, -3]
- Output
- -60
Only one triple exists, and its score is negative; that is still the answer.
Constraints
- 3 <= drifts.length <= 10^4
- -1000 <= drifts[i] <= 1000
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def largest_triple_drift(drifts: list[int]) -> int:public int largestTripleDrift(int[] drifts)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 12 test cases: 12 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's