ROTI-Prata SPOJ

Pattern: Binary search on the answer

medium

Guess a time T and count how many parathas all cooks can finish by then. A cook of rank R needs R, 2R, 3R, … minutes for the first, second, third paratha. More time only means more parathas, so binary search the least T that reaches p.

The problem

You need p parathas. There are several cooks, each with a rank R. A cook of rank R takes R minutes for their first paratha, 2R more for the second, 3R more for the third, and so on. All cooks work at the same time. Return the least number of minutes needed to make p parathas in total. For example, ranks [1, 2, 3, 4] with p = 10 give 12.

The link above is the SPOJ problem PRATA, which reads several test cases from input. This guide's function minTime solves one case, and its name is this guide's own.

Think first

  • In T minutes, how many parathas can one cook of rank R make?
  • If T minutes are enough, are T + 1?
  • What is a time that is surely enough?

Hints

    Brute force, and why it fails

    Simulate minute by minute: far too slow for large times. Handing each paratha to the cook who would finish it soonest (with a heap) works in O(p log cooks), and this guide uses it to check the answers. Binary search on time needs no simulation.

    The key insight

    Asking "how many by time T" is much easier than asking "how long for p". The first question has a monotonic answer, so binary search turns it into the second.

    Dry run

    10lo
    21 
    32 
    43 

    ranks = [1, 2, 3, 4] and p = 10. The rank-1 cook alone needs 1 + 2 + … + 10 = 55 minutes, so the answer is at most 55.

    Step 1 of 4

    The plan

    lo = 0, hi = min(ranks) * p * (p + 1) / 2
    while lo < hi:
        mid = lo + (hi - lo) / 2
        if made(mid) >= p: hi = mid
        else: lo = mid + 1
    return lo
    
    made(T): total = 0
        for each R: k = 0, used = 0
            while used + R * (k + 1) <= T: k = k + 1; used = used + R * k
            total = total + k
        return total
    

    Complexity

    Time
    O(cooks × √p × log T)
    Space
    O(1)

    Each check counts at most about √(2T/R) parathas per cook, and there are about log₂ T checks. With small ranks and p ≤ 1000, that is tiny.

    Edge cases

    • One cook: the answer is R × p(p + 1)/2.
    • One paratha: the answer is the smallest rank.
    • Many cooks of rank 1 and p equal to their number: each makes one, so the answer is 1.
    I've built my logic, show the code

    Java

    class Solution {
        public int minTime(int[] ranks, int p) {
            int best = Integer.MAX_VALUE;
            for (int r : ranks) best = Math.min(best, r);
            int lo = 0, hi = best * p * (p + 1) / 2;   // the fastest cook alone can make all p
            while (lo < hi) {
                int mid = lo + (hi - lo) / 2;
                if (made(ranks, mid) >= p) hi = mid;    // p parathas fit in mid minutes: try less
                else lo = mid + 1;
            }
            return lo;
        }
    
        private long made(int[] ranks, int time) {
            long total = 0;
            for (int r : ranks) {
                int k = 0, used = 0;                    // the (k+1)-th paratha takes r * (k + 1) minutes
                while (used + r * (k + 1) <= time) { k++; used += r * k; }
                total += k;
            }
            return total;
        }
    }

    C++

    class Solution {
    public:
        int minTime(vector<int>& ranks, int p) {
            int best = *min_element(ranks.begin(), ranks.end());
            int lo = 0, hi = best * p * (p + 1) / 2;   // the fastest cook alone can make all p
            while (lo < hi) {
                int mid = lo + (hi - lo) / 2;
                if (made(ranks, mid) >= p) hi = mid;    // p parathas fit in mid minutes: try less
                else lo = mid + 1;
            }
            return lo;
        }
    
        long long made(vector<int>& ranks, int time) {
            long long total = 0;
            for (int r : ranks) {
                int k = 0, used = 0;                    // the (k+1)-th paratha takes r * (k + 1) minutes
                while (used + r * (k + 1) <= time) { k++; used += r * k; }
                total += k;
            }
            return total;
        }
    };

    Python

    class Solution:
        def minTime(self, ranks: list[int], p: int) -> int:
            lo, hi = 0, min(ranks) * p * (p + 1) // 2   # the fastest cook alone can make all p
            while lo < hi:
                mid = (lo + hi) // 2
                if self.made(ranks, mid) >= p:
                    hi = mid                        # p parathas fit in mid minutes: try less
                else:
                    lo = mid + 1
            return lo
    
        def made(self, ranks: list[int], time: int) -> int:
            total = 0
            for r in ranks:
                k = used = 0                        # the (k+1)-th paratha takes r * (k + 1) minutes
                while used + r * (k + 1) <= time:
                    k += 1
                    used += r * k
                total += k
            return total

    Common mistakes

    • Treating every paratha as taking R minutes. The k-th one takes k × R.
    • Using a loose upper bound that overflows, such as the slowest rank times p².
    • Counting a paratha that would finish after T.

    Similar problems