Skip to content

Commit 5d983f3

Browse files
committed
Add solution and test-cases for problem 1700
1 parent 09a1dcb commit 5d983f3

File tree

3 files changed

+60
-27
lines changed

3 files changed

+60
-27
lines changed

leetcode/1601-1700/1700.Number-of-Students-Unable-to-Eat-Lunch/README.md

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
# [1700.Number of Students Unable to Eat Lunch][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers `0` and `1` respectively. All students stand in a queue. Each student either prefers square or circular sandwiches.
5+
6+
The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed in a **stack**. At each step:
7+
8+
- If the student at the front of the queue **prefers** the sandwich on the top of the stack, they will **take it** and leave the queue.
9+
- Otherwise, they will **leave it** and go to the queue's end.
10+
11+
This continues until none of the queue students want to take the top sandwich and are thus unable to eat.
12+
13+
You are given two integer arrays `students` and `sandwiches` where `sandwiches[i]` is the type of the i<sup>th</sup> sandwich in the stack (`i = 0` is the top of the stack) and `students[j]` is the preference of the j<sup>th</sup> student in the initial queue (`j = 0` is the front of the queue). Return the number of students that are unable to eat.
714

815
**Example 1:**
916

1017
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
18+
Input: students = [1,1,0,0], sandwiches = [0,1,0,1]
19+
Output: 0
20+
Explanation:
21+
- Front student leaves the top sandwich and returns to the end of the line making students = [1,0,0,1].
22+
- Front student leaves the top sandwich and returns to the end of the line making students = [0,0,1,1].
23+
- Front student takes the top sandwich and leaves the line making students = [0,1,1] and sandwiches = [1,0,1].
24+
- Front student leaves the top sandwich and returns to the end of the line making students = [1,1,0].
25+
- Front student takes the top sandwich and leaves the line making students = [1,0] and sandwiches = [0,1].
26+
- Front student leaves the top sandwich and returns to the end of the line making students = [0,1].
27+
- Front student takes the top sandwich and leaves the line making students = [1] and sandwiches = [1].
28+
- Front student takes the top sandwich and leaves the line making students = [] and sandwiches = [].
29+
Hence all students are able to eat.
1330
```
1431

15-
## 题意
16-
> ...
17-
18-
## 题解
32+
**Example 2:**
1933

20-
### 思路1
21-
> ...
22-
Number of Students Unable to Eat Lunch
23-
```go
2434
```
25-
35+
Input: students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1]
36+
Output: 3
37+
```
2638

2739
## 结语
2840

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(students []int, sandwiches []int) int {
4+
ones, zeros := 0, 0
5+
for _, n := range students {
6+
if n == 1 {
7+
ones++
8+
continue
9+
}
10+
zeros++
11+
}
12+
13+
for _, n := range sandwiches {
14+
if n == 1 {
15+
if ones == 0 {
16+
break
17+
}
18+
ones--
19+
continue
20+
}
21+
if zeros == 0 {
22+
break
23+
}
24+
zeros--
25+
}
26+
return ones + zeros
527
}

leetcode/1601-1700/1700.Number-of-Students-Unable-to-Eat-Lunch/Solution_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
students, sandwiches []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 1, 0, 0}, []int{0, 1, 0, 1}, 0},
17+
{"TestCase2", []int{1, 1, 1, 0, 0, 1}, []int{1, 0, 0, 0, 1, 1}, 3},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.students, c.sandwiches)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.students, c.sandwiches)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)