Apply credits to an invoice
Customer credits are applied against an invoice in order. Return the remaining unpaid balance.
- Apply credits in list order, each subtracting from the running balance.
- The balance is clamped at 0 — credits never produce a negative result.
CreditFirst(invoiceTotal: int, credits: list<Credit>) → int
C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
public int CreditFirst(int invoiceTotal, List<Credit> credits) {
}
Worked examples
| Call | Result |
|---|---|
CreditFirst(10000, new List<Credit> { new Credit(3000), new Credit(5000) }) | 2000 |
CreditFirst(5000, new List<Credit> { new Credit(3000) }) | 2000 |
CreditFirst(2000, new List<Credit> { new Credit(5000) }) | 0 |
CreditFirst(0, new List<Credit> { new Credit(1000) }) | 0 |
Hint
Walk the credits, subtract each, and clamp at each step.
Reference solution in C#
public int CreditFirst(int invoiceTotal, List<Credit> credits) {
int balance = invoiceTotal;
foreach (var c in credits) { balance -= c.Amount; if (balance < 0) balance = 0; }
return balance;
}