Display Table and Minimum Frogs Algorithm Problems

Display Table

Given an array of orders where each element contains a customer name, table number, and food item, return a display table showing how many of each dish was ordered at each table.

The table should have "Table" as the first column header, followed by alphabetical sorted food item names. Each row represents a table with its number and the count of each item ordered at that table. Rows should be sorted by table number in ascending order. Customer names are not included in the output.

Example 1:


Input: orders = [["David","3","Ceviche"],["Corina","10","Beef Burrito"],["David","3","Fried Chicken"],["Carla","5","Water"],["Carla","5","Ceviche"],["Rous","3","Ceviche"]]
Output: [["Table","Beef Burrito","Ceviche","Fried Chicken","Water"],["3","0","2","1","0"],["5","0","1","0","1"],["10","1","0","0","0"]]

Example 2:


Input: orders = [["James","12","Fried Chicken"],["Ratesh","12","Fried Chicken"],["Amadeus","12","Fried Chicken"],["Adam","1","Canadian Waffles"],["Brianna","1","Canadian Waffles"]]
Output: [["Table","Canadian Waffles","Fried Chicken"],["1","2","0"],["12","0","3"]]

Example 3:


Input: orders = [["Laura","2","Bean Burrito"],["John","2","Beef Burrito"],["Melissa","2","Soda"]]
Output: [["Table","Bean Burrito","Beef Burrito","Soda"],["2","1","1","1"]]

Solution


var displayTable = function(orders) {
    const tableItems = new Map();
    
    orders.forEach(([customer, tableNum, item]) => {
        if (tableItems.has(tableNum)) {
            tableItems.get(tableNum).push(item);
        } else {
            tableItems.set(tableNum, [item]);
        }
    });
    
    const allItems = [];
    tableItems.forEach(items => allItems.push(...items));
    const uniqueItems = [...new Set(allItems)].sort();
    
    const result = [];
    for (let [tableNum, items] of tableItems) {
        const counts = new Array(uniqueItems.length).fill(0);
        items.forEach(item => {
            const idx = uniqueItems.indexOf(item);
            counts[idx]++;
        });
        result.push([tableNum, ...counts.map(String)]);
    }
    
    result.sort((a, b) => Number(a[0]) - Number(b[0]));
    result.unshift(["Table", ...uniqueItems]);
    
    return result;
};

Minimum Number of Frogs

Given a string representing the mixed croak sounds of multiple frogs, where each frog must produce the sequence "croak" in order (c→r→o→a→k), return the minimum number of frogs needed to produce the given sound sequence.

If the string cannot be formed by valid "croak" sequences, return -1.

Example 1:


Input: croakOfFrogs = "croakcroak"
Output: 1

A single frog croaks twice.

Example 2:


Input: croakOfFrogs = "crcoakroak"
Output: 2

Two frogs are needed to produce this sound sequenec.

Example 3:


Input: croakOfFrogs = "croakcrook"
Output: -1

Invalid sequence - contains 'u' which is not in "croak".

Example 4:


Input: croakOfFrogs = "croakcroa"
Output: -1

Invalid sequence - incomplete croak.

Solution


var minNumberOfFrogs = function(croakOfFrogs) {
    let counters = { c: 0, r: 0, o: 0, a: 0, k: 0 };
    let maxConcurrent = 0;
    
    for (const sound of croakOfFrogs) {
        if (!(sound in counters)) return -1;
        counters[sound]++;
        
        if (counters.r > counters.c || 
            counters.o > counters.r || 
            counters.a > counters.o || 
            counters.k > counters.a) {
            return -1;
        }
        
        if (counters.k === 1) {
            counters.c--;
            counters.r--;
            counters.o--;
            counters.a--;
            counters.k--;
        }
        
        maxConcurrent = Math.max(maxConcurrent, counters.c);
    }
    
    return counters.c > 0 ? -1 : maxConcurrent;
};

Tags: javascript algorithm hashmap Sorting LeetCode

Posted on Sun, 06 Sep 2026 16:36:50 +0000 by joebarker99