Minimum Operations to Make All Array Elements Equal
Real-world version: Cost of Bringing Every Gauge to Each TargetThe statement here is our own: the same inputs, outputs and technique.
A rail of gauges reads gauges. One turn changes a single gauge by one, up or down.
Each entry of targets asks a separate question, and the rail is never actually changed: how many turns would it take to bring every gauge to that target reading?
Return the answers in the order the questions are asked.
Examples
Example 1
- Input
- gauges = [1, 2, 3], targets = [2]
- Output
- [2]
Bringing everything to two costs one turn on the 1, nothing on the 2 and one turn on the 3.
Example 2
- Input
- gauges = [5], targets = [1, 5, 9]
- Output
- [4, 0, 4]
A single gauge reading five costs four turns to reach one, nothing to reach five, and four again to reach nine.
Example 3
- Input
- gauges = [10, 10], targets = [10]
- Output
- [0]
Both gauges already read the target, so nothing needs turning.
Constraints
- 1 <= gauges.length <= 10^5
- 1 <= targets.length <= 10^5
- 1 <= gauges[i] <= 10^9
- 1 <= targets[i] <= 10^9
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 min_operations(gauges: list[int], targets: list[int]) -> list[int]:public List<Long> minOperations(int[] gauges, int[] targets)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 17 test cases: 17 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
More on the same topics
- Longest Subsequence With Limited SumReal-world version: Parcels Within Each AllowanceEasy
- Frequency of the Most Frequent ElementReal-world version: Plates at One ThicknessMedium
- Maximum Coins From K Consecutive BagsReal-world version: Richest Run of Consecutive BinsMedium
- Number of Flowers in Full BloomReal-world version: Stalls Trading When the Visitor ArrivesHard