Allocate Money by Ratio
A marketplace splits a payout of amountCents between partners in proportion to integer ratios. Shares must be whole cents and must add up to exactly amountCents.
Use the largest remainder method: partner i first gets floor(amountCents * ratios[i] / total) where total is the sum of the ratios. The cents still left over are handed out one each to the partners with the largest remainders of that division; when remainders tie, the partner with the lower index goes first.
Return the array of shares.
Example 1
Input: amountCents = 100, ratios = [1,1,1] Output: [34,33,33] Explanation: Exact shares are 33.33 each; the first partner gets the spare cent.
Example 2
Input: amountCents = 7, ratios = [1,2] Output: [2,5] Explanation: Exact shares are 2.33 and 4.67; the second has the larger remainder.
Constraints
- 0 <= amountCents <= 10^9
- 1 <= ratios.length <= 10^4
- 0 <= ratios[i] <= 10^4
- The sum of ratios is at least 1
Hint 1
Use integer arithmetic: the remainder of amountCents * ratio divided by total compares exactly.
Hint 2
Sort indexes by remainder descending, then by index ascending.
Discussion 0
Join free to post in the discussion.
Good practice for: Backend Developer, Full Stack Developer, Blockchain Developer, Data Engineer, Automation Test Engineer
Run checks your code on 3 sample tests. Submit runs all 9 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.