> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Minimize Rounding Error to Meet Target

> Tested Python solution for LeetCode 1058 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 1058, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math), [String](/catalog/topics/string), [Dynamic Programming](/catalog/topics/dynamic-programming), [Greedy](/catalog/topics/greedy), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/minimize-rounding-error-to-meet-target/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1058   # by problem number
lcpy gen -s minimize_rounding_error_to_meet_target   # by problem name
```

## Problem

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 `i` from `1` to `n`, as a string with three places after the decimal.

### Examples

```
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.
```

```
Input: prices = ["1.500","2.500","3.500"], target = 10
Output: "-1"
Explanation: It is impossible to meet the target.
```

```
Input: prices = ["1.500","2.500","3.500"], target = 9
Output: "1.500"
```

### Constraints

* 1 \<= prices.length \<= 500
* Each string prices\[i] represents a real number in the range \[0.0, 1000.0] and has exactly 3 decimal places.
* 0 \<= target \<= 10^6

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimize_rounding_error_to_meet_target/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimize_rounding_error_to_meet_target/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from decimal import Decimal


class Solution:
    # Time: O(n log n)
    # Space: O(n)
    def minimize_error(self, prices: list[str], target: int) -> str:
        floor_sum = 0
        fracs: list[Decimal] = []
        for p in prices:
            d = Decimal(p)
            floor_sum += int(d)
            if frac := d - int(d):
                fracs.append(frac)
        if not floor_sum <= target <= floor_sum + len(fracs):
            return "-1"
        ceils = target - floor_sum
        fracs.sort(reverse=True)
        error = ceils - sum(fracs[:ceils]) + sum(fracs[ceils:])
        return f"{error:.3f}"
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) | O(n) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.