Drill

ProblemsJava › pricing

Price an order across volume tiers

mediumpricingArraysMathJava

A wholesaler charges less per unit the more you buy, and the tiers stack: the first slice of the order is charged at the first rate, the next slice at the second, and so on.

tieredTotal(qty: int, tiers: list<Tier>) → int

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

Solve it in Python →

Where you start

int tieredTotal(int qty, List<Tier> tiers) {
    
}

Worked examples

CallResult
tieredTotal(60, Main.<Tier>ls(new Tier(10, 100), new Tier(50, 90), new Tier(200, 80)))5400
tieredTotal(5, Main.<Tier>ls(new Tier(10, 100), new Tier(50, 90)))500
tieredTotal(300, Main.<Tier>ls(new Tier(10, 100), new Tier(50, 90)))27100
tieredTotal(0, Main.<Tier>ls(new Tier(10, 100)))0

Hint

Track how many units you have already priced. Each tier handles at most upTo minus that.

Reference solution in Java
int tieredTotal(int qty, List<Tier> tiers) {
    if (qty <= 0 || tiers.isEmpty()) return 0;
    int done = 0, total = 0;
    for (Tier t : tiers) {
        int take = Math.min(qty - done, t.upTo - done);
        if (take > 0) { total += take * t.unitPrice; done += take; }
    }
    if (done < qty) total += (qty - done) * tiers.get(tiers.size() - 1).unitPrice;
    return total;
}

The same problem in another language

More pricing problems in Java