Skip to content

Commit 18a2fe5

Browse files
add 1418
1 parent 73ba8d1 commit 18a2fe5

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ _If you like this project, please leave me a star._ ★
99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
1111
|1419|[Reformat The String](https://leetcode.com/problems/reformat-the-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1419.java) | |Easy|String|
12+
|1418|[Display Table of Food Orders in a Restaurant](https://leetcode.com/problems/display-table-of-food-orders-in-a-restaurant/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1418.java) | |Medium|HashTable|
1213
|1415|[The k-th Lexicographical String of All Happy Strings of Length n](https://leetcode.com/problems/the-k-th-lexicographical-string-of-all-happy-strings-of-length-n/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1415.java) | |Medium|Backtracking|
1314
|1413|[Minimum Value to Get Positive Step by Step Sum](https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1413.java) | |Easy|Array|
1415
|1410|[HTML Entity Parser](https://leetcode.com/problems/html-entity-parser/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1410.java) | |Medium|String, Stack|
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.HashMap;
6+
import java.util.HashSet;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.Set;
10+
import java.util.TreeMap;
11+
12+
/**
13+
* 1418. Display Table of Food Orders in a Restaurant
14+
*
15+
* Given the array orders, which represents the orders that customers have done in a restaurant.
16+
* More specifically orders[i]=[customerNamei,tableNumberi,foodItemi] where customerNamei is the name of the customer, tableNumberi is the table customer sit at, and foodItemi is the item customer orders.
17+
* Return the restaurant's “display table”. The “display table” is a table whose row entries denote how many of each food item each table ordered.
18+
* The first column is the table number and the remaining columns correspond to each food item in alphabetical order.
19+
* The first row should be a header whose first column is “Table”, followed by the names of the food items. Note that the customer names are not part of the table. Additionally, the rows should be sorted in numerically increasing order.
20+
*
21+
* Example 1:
22+
* Input: orders = [["David","3","Ceviche"],["Corina","10","Beef Burrito"],["David","3","Fried Chicken"],["Carla","5","Water"],["Carla","5","Ceviche"],["Rous","3","Ceviche"]]
23+
* Output: [["Table","Beef Burrito","Ceviche","Fried Chicken","Water"],["3","0","2","1","0"],["5","0","1","0","1"],["10","1","0","0","0"]]
24+
* Explanation:
25+
* The displaying table looks like:
26+
* Table,Beef Burrito,Ceviche,Fried Chicken,Water
27+
* 3 ,0 ,2 ,1 ,0
28+
* 5 ,0 ,1 ,0 ,1
29+
* 10 ,1 ,0 ,0 ,0
30+
* For the table 3: David orders "Ceviche" and "Fried Chicken", and Rous orders "Ceviche".
31+
* For the table 5: Carla orders "Water" and "Ceviche".
32+
* For the table 10: Corina orders "Beef Burrito".
33+
*
34+
* Example 2:
35+
* Input: orders = [["James","12","Fried Chicken"],["Ratesh","12","Fried Chicken"],["Amadeus","12","Fried Chicken"],["Adam","1","Canadian Waffles"],["Brianna","1","Canadian Waffles"]]
36+
* Output: [["Table","Canadian Waffles","Fried Chicken"],["1","2","0"],["12","0","3"]]
37+
* Explanation:
38+
* For the table 1: Adam and Brianna order "Canadian Waffles".
39+
* For the table 12: James, Ratesh and Amadeus order "Fried Chicken".
40+
*
41+
* Example 3:
42+
* Input: orders = [["Laura","2","Bean Burrito"],["Jhon","2","Beef Burrito"],["Melissa","2","Soda"]]
43+
* Output: [["Table","Bean Burrito","Beef Burrito","Soda"],["2","1","1","1"]]
44+
*
45+
* Constraints:
46+
* 1 <= orders.length <= 5 * 10^4
47+
* orders[i].length == 3
48+
* 1 <= customerNamei.length, foodItemi.length <= 20
49+
* customerNamei and foodItemi consist of lowercase and uppercase English letters and the space character.
50+
* tableNumberi is a valid integer between 1 and 500.
51+
* */
52+
public class _1418 {
53+
public static class Solution1 {
54+
public List<List<String>> displayTable(List<List<String>> orders) {
55+
TreeMap<Integer, Map<String, Integer>> map = new TreeMap<>();
56+
Set<String> dishSet = new HashSet<>();
57+
for (List<String> order : orders) {
58+
int tableNumber = Integer.parseInt(order.get(1));
59+
String dishName = order.get(2);
60+
dishSet.add(dishName);
61+
if (!map.containsKey(tableNumber)) {
62+
map.put(tableNumber, new HashMap<>());
63+
}
64+
Map<String, Integer> dishCountMap = map.get(tableNumber);
65+
if (!dishCountMap.containsKey(dishName)) {
66+
dishCountMap.put(dishName, 1);
67+
} else {
68+
dishCountMap.put(dishName, dishCountMap.get(dishName) + 1);
69+
}
70+
}
71+
List<String> dishes = new ArrayList<>();
72+
for (String dish : dishSet) {
73+
dishes.add(dish);
74+
}
75+
Collections.sort(dishes);
76+
dishes.add(0, "Table");
77+
List<List<String>> result = new ArrayList<>();
78+
result.add(dishes);
79+
for (int tableNumber : map.keySet()) {
80+
List<String> row = new ArrayList<>();
81+
row.add("" + tableNumber);
82+
for (int i = 1; i < dishes.size(); i++) {
83+
if (map.get(tableNumber).containsKey(dishes.get(i))) {
84+
row.add(Integer.toString(map.get(tableNumber).get(dishes.get(i))));
85+
} else {
86+
row.add("0");
87+
}
88+
}
89+
result.add(row);
90+
}
91+
return result;
92+
}
93+
}
94+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1418;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _1418Test {
13+
private static _1418.Solution1 solution1;
14+
private static List<List<String>> orders;
15+
private static List<List<String>> expected;
16+
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _1418.Solution1();
20+
}
21+
22+
@Test
23+
public void test1() {
24+
orders = Arrays.asList(Arrays.asList("Laura", "2", "Bean Burrito"), Arrays.asList("Jhon", "2", "Beef Burrito"), Arrays.asList("Melissa", "2", "Soda"));
25+
expected = Arrays.asList(Arrays.asList("Table", "Bean Burrito", "Beef Burrito", "Soda"), Arrays.asList("2", "1", "1", "1"));
26+
assertEquals(expected, solution1.displayTable(orders));
27+
}
28+
29+
@Test
30+
public void test2() {
31+
orders = Arrays.asList(Arrays.asList("James", "12", "Fried Chicken"), Arrays.asList("Ratesh", "12", "Fried Chicken"), Arrays.asList("Amadeus", "12", "Fried Chicken"), Arrays.asList("Adam", "1", "Canadian Waffles"), Arrays.asList("Brianna", "1", "Canadian Waffles"));
32+
expected = Arrays.asList(Arrays.asList("Table", "Canadian Waffles", "Fried Chicken"), Arrays.asList("1", "2", "0"), Arrays.asList("12", "0", "3"));
33+
assertEquals(expected, solution1.displayTable(orders));
34+
}
35+
36+
@Test
37+
public void test3() {
38+
orders = Arrays.asList(Arrays.asList("David", "3", "Ceviche"), Arrays.asList("Corina", "10", "Beef Burrito"), Arrays.asList("David", "3", "Fried Chicken"), Arrays.asList("Carla", "5", "Water"), Arrays.asList("Carla", "5", "Ceviche"), Arrays.asList("Rous", "3", "Ceviche"));
39+
expected = Arrays.asList(Arrays.asList("Table", "Beef Burrito", "Ceviche", "Fried Chicken", "Water"), Arrays.asList("3", "0", "2", "1", "0"), Arrays.asList("5", "0", "1", "0", "1"), Arrays.asList("10", "1", "0", "0", "0"));
40+
assertEquals(expected, solution1.displayTable(orders));
41+
}
42+
43+
}

0 commit comments

Comments
 (0)