Skip to content

Commit bba0c1d

Browse files
authored
Merge pull request #928 from 0xff-dev/1418
Add solution and test-cases for problem 1418
2 parents 4f295b7 + 4e81b11 commit bba0c1d

File tree

3 files changed

+125
-9
lines changed

3 files changed

+125
-9
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# [1418.Display Table of Food Orders in a Restaurant][title]
2+
3+
## Description
4+
Given the array `orders`, which represents the orders that customers have done in a restaurant. 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.
5+
6+
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. The first column is the table number and the remaining columns correspond to each food item in alphabetical order. 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.
7+
8+
**Example 1:**
9+
10+
```
11+
Input: orders = [["David","3","Ceviche"],["Corina","10","Beef Burrito"],["David","3","Fried Chicken"],["Carla","5","Water"],["Carla","5","Ceviche"],["Rous","3","Ceviche"]]
12+
Output: [["Table","Beef Burrito","Ceviche","Fried Chicken","Water"],["3","0","2","1","0"],["5","0","1","0","1"],["10","1","0","0","0"]]
13+
Explanation:
14+
The displaying table looks like:
15+
Table,Beef Burrito,Ceviche,Fried Chicken,Water
16+
3 ,0 ,2 ,1 ,0
17+
5 ,0 ,1 ,0 ,1
18+
10 ,1 ,0 ,0 ,0
19+
For the table 3: David orders "Ceviche" and "Fried Chicken", and Rous orders "Ceviche".
20+
For the table 5: Carla orders "Water" and "Ceviche".
21+
For the table 10: Corina orders "Beef Burrito".
22+
```
23+
24+
**Example 2:**
25+
26+
```
27+
Input: orders = [["James","12","Fried Chicken"],["Ratesh","12","Fried Chicken"],["Amadeus","12","Fried Chicken"],["Adam","1","Canadian Waffles"],["Brianna","1","Canadian Waffles"]]
28+
Output: [["Table","Canadian Waffles","Fried Chicken"],["1","2","0"],["12","0","3"]]
29+
Explanation:
30+
For the table 1: Adam and Brianna order "Canadian Waffles".
31+
For the table 12: James, Ratesh and Amadeus order "Fried Chicken".
32+
```
33+
34+
**Example 3:**
35+
36+
```
37+
Input: orders = [["Laura","2","Bean Burrito"],["Jhon","2","Beef Burrito"],["Melissa","2","Soda"]]
38+
Output: [["Table","Bean Burrito","Beef Burrito","Soda"],["2","1","1","1"]]
39+
```
40+
41+
## 结语
42+
43+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
44+
45+
[title]: https://leetcode.com/problems/display-table-of-food-orders-in-a-restaurant
46+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,46 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"sort"
5+
"strconv"
6+
)
7+
8+
func Solution(orders [][]string) [][]string {
9+
foodHeaders := make([]string, 0)
10+
foodHeaderIndex := make(map[string]struct{})
11+
tables := make([]string, 0)
12+
tableFood := make(map[string]map[string]int)
13+
14+
for _, order := range orders {
15+
t, f := order[1], order[2]
16+
if _, ok := foodHeaderIndex[f]; !ok {
17+
foodHeaders = append(foodHeaders, f)
18+
foodHeaderIndex[f] = struct{}{}
19+
}
20+
21+
if _, ok := tableFood[t]; !ok {
22+
tables = append(tables, t)
23+
tableFood[t] = make(map[string]int)
24+
}
25+
tableFood[t][f]++
26+
}
27+
sort.Strings(foodHeaders)
28+
foodHeaders = append([]string{"Table"}, foodHeaders...)
29+
sort.Slice(tables, func(i, j int) bool {
30+
a, _ := strconv.Atoi(tables[i])
31+
b, _ := strconv.Atoi(tables[j])
32+
return a < b
33+
})
34+
35+
ans := make([][]string, 0)
36+
ans = append(ans, foodHeaders)
37+
for _, t := range tables {
38+
line := make([]string, len(foodHeaders))
39+
line[0] = t
40+
for i := 1; i < len(line); i++ {
41+
line[i] = strconv.Itoa(tableFood[t][foodHeaders[i]])
42+
}
43+
ans = append(ans, line)
44+
}
45+
return ans
546
}

leetcode/1401-1500/1418.Display-Table-of-Food-Orders-in-a-Restaurant/Solution_test.go

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,41 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]string
14+
expect [][]string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]string{
17+
{"David", "3", "Ceviche"},
18+
{"Corina", "10", "Beef Burrito"},
19+
{"David", "3", "Fried Chicken"},
20+
{"Carla", "5", "Water"},
21+
{"Carla", "5", "Ceviche"},
22+
{"Rous", "3", "Ceviche"},
23+
}, [][]string{
24+
{"Table", "Beef Burrito", "Ceviche", "Fried Chicken", "Water"},
25+
{"3", "0", "2", "1", "0"},
26+
{"5", "0", "1", "0", "1"},
27+
{"10", "1", "0", "0", "0"},
28+
}},
29+
{"TestCase2", [][]string{
30+
{"James", "12", "Fried Chicken"},
31+
{"Ratesh", "12", "Fried Chicken"},
32+
{"Amadeus", "12", "Fried Chicken"},
33+
{"Adam", "1", "Canadian Waffles"},
34+
{"Brianna", "1", "Canadian Waffles"},
35+
}, [][]string{
36+
{"Table", "Canadian Waffles", "Fried Chicken"},
37+
{"1", "2", "0"},
38+
{"12", "0", "3"},
39+
}},
40+
{"TestCase3", [][]string{
41+
{"Laura", "2", "Bean Burrito"},
42+
{"Jhon", "2", "Beef Burrito"},
43+
{"Melissa", "2", "Soda"},
44+
}, [][]string{
45+
{"Table", "Bean Burrito", "Beef Burrito", "Soda"},
46+
{"2", "1", "1", "1"},
47+
}},
1948
}
2049

2150
// 开始测试
@@ -30,10 +59,10 @@ func TestSolution(t *testing.T) {
3059
}
3160
}
3261

33-
// 压力测试
62+
// 压力测试
3463
func BenchmarkSolution(b *testing.B) {
3564
}
3665

37-
// 使用案列
66+
// 使用案列
3867
func ExampleSolution() {
3968
}

0 commit comments

Comments
 (0)