Problem · Greedy

Round Prices to Match Target

Learn this problem
MediumAirbnb logoAirbnbINTERNOA

Problem statement

Given a float array prices and an integer target, round every price to either its floor or its ceiling so that the rounded values sum to target while minimizing the total absolute rounding error.

For example, prices = [1.2, 4.3, 5.8, 6.4] and target = 18 returns [1, 4, 6, 7].

Practice rule

  • If equal fractional parts compete for the last round-up position, prefer the smaller original index.
  • If no floor-or-ceiling assignment can sum to target, return an empty array.

Function

roundPricesToMatchTarget(prices: float[], target: int) → int[]

Examples

Example 1

prices = [1.2, 4.3, 5.8, 6.4]target = 18return = [1, 4, 6, 7]
:)

More Airbnb problems

drafts saved locally
public int[] roundPricesToMatchTarget(float[] prices, int target) {
  // write your code here
}
prices[1.2, 4.3, 5.8, 6.4]
target18
expected[1, 4, 6, 7]
checking account