Minimum Fuel Car Alignment
Updated
Problem statement
You are given the positions of n cars. Car i is at (xCoordinates[i], yCoordinates[i]), and every coordinate is an integer.
Move every car so that the cars end up side by side in one horizontal row, with no gaps. You choose the integer row y and the integer starting position s. The cars then fill the spots (s, y), (s + 1, y), ..., (s + n - 1, y), one car per spot, and you decide which car goes to which spot.
Moving a car from (x1, y1) to (x2, y2) costs |x1 - x2| + |y1 - y2| units of fuel.
Return the smallest possible total fuel cost. The answer is a 64-bit integer.
Function
minimumFuelCost(xCoordinates: int[], yCoordinates: int[]) → longExamples
Example 1
xCoordinates = [1,4]yCoordinates = [1,4]return = 5Put the row at y = 1 and use the x-positions [1, 2]. The first car is already in place, so it costs 0. The second car costs |4 - 2| + |4 - 1| = 5. No other layout is cheaper, so the answer is 5.
Example 2
xCoordinates = [0,5,2]yCoordinates = [2,2,2]return = 3All cars are already in row y = 2, so keep that row. Use the x-positions [1, 2, 3]. Sorted, the cars' x-coordinates are [0, 2, 5]. Match them in that order: the moves cost 1 + 0 + 2 = 3, which is the answer.
Example 3
xCoordinates = [4,1,3,2]yCoordinates = [3,0,2,1]return = 4The x-coordinates already fill four spots in a row with no gap, so no car moves sideways. Put the row at y = 1. The up and down moves cost 2 + 1 + 1 + 0 = 4, which is the answer.
Constraints
1 <= n <= 2 * 10^5xCoordinates.length == yCoordinates.length == n-10^9 <= xCoordinates[i], yCoordinates[i] <= 10^9- Every final coordinate is an integer, and each of the
nconsecutive final positions is occupied by exactly one car. - The minimum total cost fits in a signed 64-bit integer.
More Fivetran interview questions
Similar questions at other companies
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Delete and Freeze GameRubrik · OARubrik interview questions
- Maximum Three-Server Difficulty ScoreVisa · OAVisa interview questions
- Fair Capped Money DistributionGusto · PHONE SCREEN · ONSITE INTERVIEWGusto interview questions
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Apartment Housemate PreferencesGoogle · PHONE SCREENGoogle interview questions
- Adaptive Calibration ErrorOpenAI · PHONE SCREENOpenAI interview questions
- Maximum Number of Events That Can Be AttendedByteDance · PHONE SCREENByteDance interview questions
- Remove the Fewest Magic BeansThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
- Aggressive CowsAmazon · PHONE SCREEN
Fivetran interview experiences
- Fivetran Staff SRE Hiring Manager Interview: 1 Experience
- Fivetran Software Engineer HackerRank and Java: 1 Experience
- Fivetran Senior SRE Interview Process: 1 Experience
- Fivetran Software Engineer HashMap and Design: 1 Experience
- Fivetran SDE-2 Coding and Parking Lot Design: 1 Experience
- Fivetran Software Engineer Interview, Toronto: 1 Experience