Skip to content

Commit f2be975

Browse files
committed
feat: add solutions to lc problem: No.1700
No.1700.Number of Students Unable to Eat Lunch
1 parent 369c416 commit f2be975

File tree

7 files changed

+145
-37
lines changed

7 files changed

+145
-37
lines changed

solution/1000-1099/1092.Shortest Common Supersequence/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ str2 = "cab" 是 "cabac" 的一个子串,因为我们可
4242
定义 $f[i][j]$ 表示字符串 $str1$ 的前 $i$ 个字符和字符串 $str2$ 的前 $j$ 个字符的最长公共子序列的长度。状态转移方程如下:
4343

4444
$$
45-
f[i][j] = \left\{\begin{matrix}
45+
f[i][j] =
46+
\begin{cases}
4647
0 & i = 0 \text{ or } j = 0 \\
4748
f[i - 1][j - 1] + 1 & str1[i - 1] = str2[j - 1] \\
4849
\max(f[i - 1][j], f[i][j - 1]) & str1[i - 1] \neq str2[j - 1]
49-
\end{matrix}\right.
50+
\end{cases}
5051
$$
5152

5253
接下来我们基于 $f[i][j]$ 构造出最短公共超序列。

solution/1700-1799/1700.Number of Students Unable to Eat Lunch/README.md

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,15 @@
5757

5858
<!-- 这里可写通用的实现逻辑 -->
5959

60-
学生位置可调整,而三明治位置不可调整。也就是说,若前面的三明治没被拿走,则往后的所有三明治也无法被拿走。
60+
**方法一:计数**
6161

62-
因此,先用计数器 counter 统计学生喜欢的三明治种类和对应的数量,然后遍历三明治,若在 counter 中找不到喜欢此三明治的学生,说明已经找到答案,当前以及往后的三明治均无法被拿走,数量为 `n - i`
62+
我们观察发现,学生位置可调整,而三明治位置不可调整。也就是说,若前面的三明治没被拿走,则往后的所有三明治也无法被拿走。
63+
64+
因此,我们先用计数器 `cnt` 统计学生喜欢的三明治种类和对应的数量。
65+
66+
然后遍历三明治,若在 `cnt` 中找不到喜欢此三明治的学生,说明已经找到答案,当前以及往后的三明治均无法被拿走,数量为 $n - i$。
67+
68+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为三明治数量。
6369

6470
<!-- tabs:start -->
6571

@@ -70,11 +76,11 @@
7076
```python
7177
class Solution:
7278
def countStudents(self, students: List[int], sandwiches: List[int]) -> int:
73-
counter = Counter(students)
74-
for i, sandwich in enumerate(sandwiches):
75-
if counter[sandwich] == 0:
79+
cnt = Counter(students)
80+
for i, v in enumerate(sandwiches):
81+
if cnt[v] == 0:
7682
return len(students) - i
77-
counter[sandwich] -= 1
83+
cnt[v] -= 1
7884
return 0
7985
```
8086

@@ -85,18 +91,55 @@ class Solution:
8591
```java
8692
class Solution {
8793
public int countStudents(int[] students, int[] sandwiches) {
88-
int[] counter = new int[2];
89-
for (int i : students) {
90-
counter[i] += 1;
94+
int[] cnt = new int[2];
95+
for (int v : students) {
96+
++cnt[v];
9197
}
92-
for (int i = 0; i < sandwiches.length; ++i) {
93-
if (counter[sandwiches[i]] == 0) {
94-
return sandwiches.length - i;
98+
int n = students.length;
99+
for (int i = 0; i < n; ++i) {
100+
if (cnt[sandwiches[i]]-- == 0) {
101+
return n - i;
102+
}
103+
}
104+
return 0;
105+
}
106+
}
107+
```
108+
109+
### **C++**
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
int countStudents(vector<int>& students, vector<int>& sandwiches) {
115+
int cnt[2] = {0};
116+
for (int& v : students) ++cnt[v];
117+
int n = students.size();
118+
for (int i = 0; i < n; ++i) {
119+
if (cnt[sandwiches[i]]-- == 0) {
120+
return n - i;
95121
}
96-
counter[sandwiches[i]] -= 1;
97122
}
98123
return 0;
99124
}
125+
};
126+
```
127+
128+
### **Go**
129+
130+
```go
131+
func countStudents(students []int, sandwiches []int) int {
132+
cnt := [2]int{}
133+
for _, v := range students {
134+
cnt[v]++
135+
}
136+
for i, v := range sandwiches {
137+
if cnt[v] == 0 {
138+
return len(students) - i
139+
}
140+
cnt[v]--
141+
}
142+
return 0
100143
}
101144
```
102145

solution/1700-1799/1700.Number of Students Unable to Eat Lunch/README_EN.md

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ Hence all students are able to eat.
6161
```python
6262
class Solution:
6363
def countStudents(self, students: List[int], sandwiches: List[int]) -> int:
64-
counter = Counter(students)
65-
for i, sandwich in enumerate(sandwiches):
66-
if counter[sandwich] == 0:
64+
cnt = Counter(students)
65+
for i, v in enumerate(sandwiches):
66+
if cnt[v] == 0:
6767
return len(students) - i
68-
counter[sandwich] -= 1
68+
cnt[v] -= 1
6969
return 0
7070
```
7171

@@ -74,21 +74,58 @@ class Solution:
7474
```java
7575
class Solution {
7676
public int countStudents(int[] students, int[] sandwiches) {
77-
int[] counter = new int[2];
78-
for (int i : students) {
79-
counter[i] += 1;
77+
int[] cnt = new int[2];
78+
for (int v : students) {
79+
++cnt[v];
8080
}
81-
for (int i = 0; i < sandwiches.length; ++i) {
82-
if (counter[sandwiches[i]] == 0) {
83-
return sandwiches.length - i;
81+
int n = students.length;
82+
for (int i = 0; i < n; ++i) {
83+
if (cnt[sandwiches[i]]-- == 0) {
84+
return n - i;
8485
}
85-
counter[sandwiches[i]] -= 1;
8686
}
8787
return 0;
8888
}
8989
}
9090
```
9191

92+
### **C++**
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
int countStudents(vector<int>& students, vector<int>& sandwiches) {
98+
int cnt[2] = {0};
99+
for (int& v : students) ++cnt[v];
100+
int n = students.size();
101+
for (int i = 0; i < n; ++i) {
102+
if (cnt[sandwiches[i]]-- == 0) {
103+
return n - i;
104+
}
105+
}
106+
return 0;
107+
}
108+
};
109+
```
110+
111+
### **Go**
112+
113+
```go
114+
func countStudents(students []int, sandwiches []int) int {
115+
cnt := [2]int{}
116+
for _, v := range students {
117+
cnt[v]++
118+
}
119+
for i, v := range sandwiches {
120+
if cnt[v] == 0 {
121+
return len(students) - i
122+
}
123+
cnt[v]--
124+
}
125+
return 0
126+
}
127+
```
128+
92129
### **...**
93130

94131
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int countStudents(vector<int>& students, vector<int>& sandwiches) {
4+
int cnt[2] = {0};
5+
for (int& v : students) ++cnt[v];
6+
int n = students.size();
7+
for (int i = 0; i < n; ++i) {
8+
if (cnt[sandwiches[i]]-- == 0) {
9+
return n - i;
10+
}
11+
}
12+
return 0;
13+
}
14+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func countStudents(students []int, sandwiches []int) int {
2+
cnt := [2]int{}
3+
for _, v := range students {
4+
cnt[v]++
5+
}
6+
for i, v := range sandwiches {
7+
if cnt[v] == 0 {
8+
return len(students) - i
9+
}
10+
cnt[v]--
11+
}
12+
return 0
13+
}

solution/1700-1799/1700.Number of Students Unable to Eat Lunch/Solution.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
class Solution {
22
public int countStudents(int[] students, int[] sandwiches) {
3-
int[] counter = new int[2];
4-
for (int i : students) {
5-
counter[i] += 1;
3+
int[] cnt = new int[2];
4+
for (int v : students) {
5+
++cnt[v];
66
}
7-
for (int i = 0; i < sandwiches.length; ++i) {
8-
if (counter[sandwiches[i]] == 0) {
9-
return sandwiches.length - i;
7+
int n = students.length;
8+
for (int i = 0; i < n; ++i) {
9+
if (cnt[sandwiches[i]]-- == 0) {
10+
return n - i;
1011
}
11-
counter[sandwiches[i]] -= 1;
1212
}
1313
return 0;
1414
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution:
22
def countStudents(self, students: List[int], sandwiches: List[int]) -> int:
3-
counter = Counter(students)
4-
for i, sandwich in enumerate(sandwiches):
5-
if counter[sandwich] == 0:
3+
cnt = Counter(students)
4+
for i, v in enumerate(sandwiches):
5+
if cnt[v] == 0:
66
return len(students) - i
7-
counter[sandwich] -= 1
7+
cnt[v] -= 1
88
return 0

0 commit comments

Comments
 (0)