Check if it is possible to survive on Island

Pattern: Candy and assign cookies

medium

You need S × M food in total and can buy at most N a day on shop days. Every seventh day the shop is shut, so in a full week you must be able to buy enough for seven days in six. If that or N ≥ M fails, it is impossible; otherwise buy as early as possible and the days needed are ceil(S × M / N).

The problem

You must survive S days. The only shop sells at most N units of food a day and you eat M a day. You arrive on a Monday, the shop is shut on Sundays (day 7, 14, …), and food keeps. Return the fewest days on which you must buy food, or -1 if you cannot survive. S = 10, N = 16, M = 2 gives 2. The linked page could not be read when this guide was written, so the rules below are this guide's own statement of the task.

Think first

  • How much food do you need in total?
  • What can go wrong even if the total is fine?
  • When should you buy?

Hints

    Brute force, and why it fails

    Simulate each possible number of buying days: O(S²).

    The key insight

    If the weekly and daily limits are met, buying N from day 1 onwards never runs out, so the count is just the total need divided by N, rounded up.

    Dry run

    need20

    S = 10 days, N = 16 a day can be bought, M = 2 needed a day. Total need 20.

    Step 1 of 3

    The plan

    if N < M or (S > 6 and 6N < 7M) or (S - S / 7) × N < S × M: return -1
    return ceil(S × M / N)
    

    Complexity

    Time
    O(1)
    Space
    O(1)

    A few arithmetic checks.

    Edge cases

    • S less than 7: no Sunday falls inside.
    • N equal to M with a Sunday: impossible.
    • N much larger than M: very few days.
    I've built my logic, show the code

    Java

    class Solution {
        public int minimumDays(int S, int N, int M) {
            int sundays = S / 7;
            int buyingDays = S - sundays;           // the shop is shut every seventh day
            long need = (long) S * M;
            if ((long) buyingDays * N < need || (S > 6 && 6L * N < 7L * M) || N < M) return -1;   // cannot keep up
            return (int) ((need + N - 1) / N);      // buy N a day, as early as possible
        }
    }

    C++

    class Solution {
    public:
        int minimumDays(int S, int N, int M) {
            int sundays = S / 7;
            int buyingDays = S - sundays;           // the shop is shut every seventh day
            long long need = static_cast<long long>(S) * M;
            if (static_cast<long long>(buyingDays) * N < need || (S > 6 && 6LL * N < 7LL * M) || N < M) return -1;   // cannot keep up
            return static_cast<int>((need + N - 1) / N);   // buy N a day, as early as possible
        }
    };

    Python

    class Solution:
        def minimumDays(self, S: int, N: int, M: int) -> int:
            buying_days = S - S // 7                # the shop is shut every seventh day
            need = S * M
            if buying_days * N < need or (S > 6 and 6 * N < 7 * M) or N < M:
                return -1                           # cannot keep up
            return -(-need // N)                    # buy N a day, as early as possible

    Common mistakes

    • Forgetting the Sundays.
    • Rounding down the number of days.

    Similar problems