Picking Up Chicks

Pattern: Candy and assign cookies

medium

A chick reaches the barn on time if X + V × T ≥ B. A fast chick stuck behind a slow one must be lifted over it (one swap per slow chick ahead). Work from the chick nearest the barn backwards: each chick that can make it costs the number of slow chicks already passed; stop after K.

The problem

Chicks run along a line towards a barn at B. Chick i starts at x[i] (increasing) with speed v[i]. A chick that catches a slower one must slow down, unless you lift it over (one swap). Return the fewest swaps so that at least K chicks reach the barn within time T, or -1 if that is impossible. The Code Jam version prints IMPOSSIBLE; here it is -1. The function takes (K, B, T, x, v).

Think first

  • Which chicks can reach the barn if nothing blocks them?
  • Which chicks can block such a chick?
  • Which K chicks should you choose?

Hints

    Brute force, and why it fails

    Try every set of K chicks and count swaps for each: combinations.

    The key insight

    Choosing the K capable chicks closest to the barn minimises the slow chicks ahead of each one, and each slow chick ahead costs exactly one swap.

    Dry run

    canyes no no yes yes

    K = 3, barn 10, time 5, X = [0, 2, 3, 5, 7], V = [2, 1, 1, 1, 4]. On time: chicks at 0, 5 and 7.

    Step 1 of 4

    The plan

    for i from n - 1 down to 0, while done < K:
        if x[i] + v[i] × T >= B: swaps += slowAhead; done += 1
        else: slowAhead += 1
    return swaps if done == K else -1
    

    Complexity

    Time
    O(n)
    Space
    O(1)

    One pass from the barn end.

    Edge cases

    • K = 0: 0 swaps.
    • Not enough capable chicks: -1.
    • No slow chicks: 0.
    I've built my logic, show the code

    Java

    class Solution {
        public int minSwaps(int k, int barn, int time, int[] x, int[] v) {
            int done = 0, slowAhead = 0, swaps = 0;
            for (int i = x.length - 1; i >= 0 && done < k; i--) {   // from the chick nearest the barn backwards
                if ((long) x[i] + (long) v[i] * time >= barn) {
                    swaps += slowAhead;             // it must be lifted over every slow chick in front
                    done++;
                } else {
                    slowAhead++;                    // this one cannot make it, and blocks the ones behind
                }
            }
            return done == k ? swaps : -1;
        }
    }

    C++

    class Solution {
    public:
        int minSwaps(int k, int barn, int time, vector<int>& x, vector<int>& v) {
            int done = 0, slowAhead = 0, swaps = 0;
            for (int i = static_cast<int>(x.size()) - 1; i >= 0 && done < k; i--) {   // nearest the barn first
                if (static_cast<long long>(x[i]) + static_cast<long long>(v[i]) * time >= barn) {
                    swaps += slowAhead;             // it must be lifted over every slow chick in front
                    done++;
                } else {
                    slowAhead++;                    // this one cannot make it, and blocks the ones behind
                }
            }
            return done == k ? swaps : -1;
        }
    };

    Python

    class Solution:
        def minSwaps(self, k: int, barn: int, time: int, x: list[int], v: list[int]) -> int:
            done = slow_ahead = swaps = 0
            for i in range(len(x) - 1, -1, -1):     # from the chick nearest the barn backwards
                if done == k:
                    break
                if x[i] + v[i] * time >= barn:
                    swaps += slow_ahead             # it must be lifted over every slow chick in front
                    done += 1
                else:
                    slow_ahead += 1                 # this one cannot make it, and blocks the ones behind
            return swaps if done == k else -1

    Common mistakes

    • Counting swaps for capable chicks that are ahead (they do not block).
    • Scanning from the far end.

    Similar problems