Function to find Number of customers who could not get a computer

Pattern: Hashing and frequency

medium

Simulate the café with a small state per customer (away, using a computer, or turned away but still inside), counting the ones who find no free computer.

The problem

A café has n computers. You are given a string of capital letters, where each letter is one customer: its first appearance is that customer arriving and its second is that customer leaving. Every letter appears exactly twice. A customer who arrives when all computers are busy is turned away (but their departure still appears later). Customers who got a computer free it when they leave. Return how many customers were turned away. For example, with n = 2 and "ABBAJJKZKZ" nobody is turned away, while with n = 1 and "ABCBCA" two customers are.

This problem is from Love Babbar's 450 sheet. Its main link points to GeeksforGeeks, which this site has not been able to check automatically. The function name runCustomerSimulation is this guide's own.

Think first

  • When you read a letter, how do you know whether this is an arrival or a departure?
  • A turned-away customer leaves later too. What must not happen when they do?
  • How much memory do you need for 26 possible customers?

Hints

    Brute force, and why it fails

    There is no slow method to avoid here: one pass over the string is needed and enough. The only difficulty is keeping the states straight, which is why writing them out (0, 1, 2) helps.

    The key insight

    A direct simulation is enough, as long as each customer's state tells you what their next appearance means. With only 26 customers, an array of 26 states is the whole memory.

    Dry run

    A0 
    B1 
    C2 
    B3 
    C4 
    A5 
    occupied0
    turnedAway0

    There is n = 1 computer and the sequence is ABCBCA. Each letter's first appearance is its arrival and its second is its departure.

    Step 1 of 6

    The plan

    state = [0] * 26          # 0 not here, 1 using a computer, 2 turned away but inside
    occupied = 0
    turnedAway = 0
    for letter in seq:
        if state[letter] == 0:            # arriving
            if occupied < n:
                state[letter] = 1; occupied = occupied + 1
            else:
                state[letter] = 2; turnedAway = turnedAway + 1
        else:                             # leaving
            if state[letter] == 1:
                occupied = occupied - 1
            state[letter] = 0
    return turnedAway
    

    Complexity

    Time
    O(L)
    Space
    O(1)

    L is the length of the sequence (at most 52). The state array always has 26 entries.

    Edge cases

    • n = 0: every customer is turned away.
    • n at least 26: nobody is turned away.
    • A turned-away customer leaving must not free a computer they never had.
    I've built my logic, show the code

    Java

    class Solution {
        public int runCustomerSimulation(int n, String seq) {
            int[] state = new int[26];          // 0 = not here, 1 = using a computer, 2 = turned away and still inside
            int occupied = 0;
            int turnedAway = 0;
            for (char ch : seq.toCharArray()) {
                int c = ch - 'A';
                if (state[c] == 0) {            // arriving
                    if (occupied < n) {
                        state[c] = 1;
                        occupied++;
                    } else {
                        state[c] = 2;
                        turnedAway++;
                    }
                } else {                        // leaving
                    if (state[c] == 1) {
                        occupied--;             // frees a computer
                    }
                    state[c] = 0;
                }
            }
            return turnedAway;
        }
    }

    C++

    class Solution {
    public:
        int runCustomerSimulation(int n, string seq) {
            int state[26] = {0};                // 0 = not here, 1 = using a computer, 2 = turned away and still inside
            int occupied = 0;
            int turnedAway = 0;
            for (char ch : seq) {
                int c = ch - 'A';
                if (state[c] == 0) {            // arriving
                    if (occupied < n) {
                        state[c] = 1;
                        occupied++;
                    } else {
                        state[c] = 2;
                        turnedAway++;
                    }
                } else {                        // leaving
                    if (state[c] == 1) {
                        occupied--;             // frees a computer
                    }
                    state[c] = 0;
                }
            }
            return turnedAway;
        }
    };

    Python

    class Solution:
        def runCustomerSimulation(self, n, seq):
            state = [0] * 26                    # 0 = not here, 1 = using a computer, 2 = turned away and still inside
            occupied = 0
            turned_away = 0
            for ch in seq:
                c = ord(ch) - ord("A")
                if state[c] == 0:               # arriving
                    if occupied < n:
                        state[c] = 1
                        occupied += 1
                    else:
                        state[c] = 2
                        turned_away += 1
                else:                           # leaving
                    if state[c] == 1:
                        occupied -= 1           # frees a computer
                    state[c] = 0
            return turned_away

    Common mistakes

    • Freeing a computer when a turned-away customer leaves, which lets later customers in wrongly.
    • Treating a letter's second appearance as another arrival.
    • Counting a customer as turned away twice.

    Similar problems