Trains the technique from
LeetCode 163Sum ClosestThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A submersible carries a rack of ballast pods. Releasing a pod shifts the craft's trim by a signed amount, listed in pods: a positive amount lifts the nose, a negative amount drops it.
The pilot has to release exactly three pods, taken from three different rack positions, and wants the shifts to add up as near as possible to goal.
Return that combined shift, not the positions. Two different choices can land the same distance away on opposite sides of goal; when that happens, return the smaller of the two totals.
Example 1
Releasing the pods worth -4, 3 and 9 shifts trim by 8, one short of the goal. The other three choices land at 11, 17 and 24, all further off.
Example 2
Two choices sit three away: -4, -3 and 9 total 2, while -7, 6 and 9 total 8. Nothing lands nearer, so the tie rule hands back the smaller total.
Example 3
The rack cannot drop the nose anywhere near that far, so the lowest reachable total, -3000, is the closest it can get.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def closest_trim_total(pods: list[int], goal: int) -> int:public int closestTrimTotal(int[] pods, int goal)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.