原题说明
Given an array of prices [p1,p2…,pn] and a target, round each price pi to Roundi(pi) so that the rounded array [Round1(p1),Round2(p2)…,Roundn(pn)] sums to the given target. Each operation Roundi(pi) could be either Floor(pi) or Ceil(pi).
Return the string “-1” if the rounded array is impossible to sum to target. Otherwise, return the smallest rounding error, which is defined as Σ |Roundi(pi) - (pi)| for
Example 1:
Input: prices = [“0.700”,”2.800”,”4.900”], target = 8
Output: “1.000”
Explanation:
Use Floor, Ceil and Ceil operations to get (0.7 - 0) + (3 - 2.8) + (5 - 4.9) = 0.7 + 0.2 + 0.1 = 1.0 .
Example 2:
Input: prices = [“1.500”,”2.500”,”3.500”], target = 10
Output: “-1”
Explanation:
It is impossible to meet the target.
Note:
1 <= prices.length <= 500.- Each string of prices
prices[i]represents a real number which is between 0 and 1000 and has exactly 3 decimal places. targetis between 0 and 1000000.
解题思路
如果一个数字是一个整数, 那么我们只能取floor,不能取ceil。这相当于一个无法调整的数字,否则就是一个可调整的数字。我们把所有可调整的数字的小数部分放入一个priority queue中,把priority queue的size记为pqsize。
然后我们先判断什么情况下无法得到target:
- 如果取最小的可能的和,那么所有数字都要取floor。如果这个和仍然比target大,或者比
target-pqsize小,那么就说明无论如何也不可能得到target。这样我们就返回“-1” - 若满足上述条件,我们一定可以取到满足题目条件的和。我们需要知道调整多少个数字,即把floor操作变成ceil操作。需要调整的数字个数等于
target-pqsize。 - 为了的达到最小的
rounding error,对于每个调整的操作,我们希望它们小数尽可能大,这可以由之前的priority queue得到。取那个数字的ceil。最后把所有不需要调整的小数也加上,就是最小的rounding error了。
注意最后返回字符串是,需要做些特殊处理,只保留最后3位小数即可。
示例代码 (cpp)
1 | class Solution { |
示例代码 (java)
1 | class Solution { |
示例代码 (python)
1 | class Solution: |
复杂度分析
时间复杂度: O(NlogN)
空间复杂度: O(N)
归纳总结
我们在Youtube上更新了视频讲解,欢迎关注!