Skip to content

Commit 3d82a5a

Browse files
committed
feat: add solutions to lc problem: No.1700. Number of Studnets Unable to Eat Lunch
1 parent 043bcf9 commit 3d82a5a

File tree

7 files changed

+78
-9
lines changed

7 files changed

+78
-9
lines changed

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

+27-3
Original file line numberDiff line numberDiff line change
@@ -53,27 +53,51 @@
5353
<li><code>students[i]</code> 要么是 <code>0</code> ,要么是 <code>1</code> 。</li>
5454
</ul>
5555

56-
5756
## 解法
5857

5958
<!-- 这里可写通用的实现逻辑 -->
6059

60+
学生位置可调整,而三明治位置不可调整。也就是说,若前面的三明治没被拿走,则往后的所有三明治也无法被拿走。
61+
62+
因此,先用计数器 counter 统计学生喜欢的三明治种类和对应的数量,然后遍历三明治,若在 counter 中找不到喜欢此三明治的学生,说明已经找到答案,当前以及往后的三明治均无法被拿走,数量为 `n - i`
63+
6164
<!-- tabs:start -->
6265

6366
### **Python3**
6467

6568
<!-- 这里可写当前语言的特殊实现逻辑 -->
6669

6770
```python
68-
71+
class Solution:
72+
def countStudents(self, students: List[int], sandwiches: List[int]) -> int:
73+
counter = collections.Counter(students)
74+
for i, sandwich in enumerate(sandwiches):
75+
if counter[sandwich] == 0:
76+
return len(students) - i
77+
counter[sandwich] -= 1
78+
return 0
6979
```
7080

7181
### **Java**
7282

7383
<!-- 这里可写当前语言的特殊实现逻辑 -->
7484

7585
```java
76-
86+
class Solution {
87+
public int countStudents(int[] students, int[] sandwiches) {
88+
int[] counter = new int[2];
89+
for (int i : students) {
90+
counter[i] += 1;
91+
}
92+
for (int i = 0; i < sandwiches.length; ++i) {
93+
if (counter[sandwiches[i]] == 0) {
94+
return sandwiches.length - i;
95+
}
96+
counter[sandwiches[i]] -= 1;
97+
}
98+
return 0;
99+
}
100+
}
77101
```
78102

79103
### **...**

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

+23-3
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,41 @@ Hence all students are able to eat.
5252
<li><code>students[i]</code> is <code>0</code> or <code>1</code>.</li>
5353
</ul>
5454

55-
5655
## Solutions
5756

5857
<!-- tabs:start -->
5958

6059
### **Python3**
6160

6261
```python
63-
62+
class Solution:
63+
def countStudents(self, students: List[int], sandwiches: List[int]) -> int:
64+
counter = collections.Counter(students)
65+
for i, sandwich in enumerate(sandwiches):
66+
if counter[sandwich] == 0:
67+
return len(students) - i
68+
counter[sandwich] -= 1
69+
return 0
6470
```
6571

6672
### **Java**
6773

6874
```java
69-
75+
class Solution {
76+
public int countStudents(int[] students, int[] sandwiches) {
77+
int[] counter = new int[2];
78+
for (int i : students) {
79+
counter[i] += 1;
80+
}
81+
for (int i = 0; i < sandwiches.length; ++i) {
82+
if (counter[sandwiches[i]] == 0) {
83+
return sandwiches.length - i;
84+
}
85+
counter[sandwiches[i]] -= 1;
86+
}
87+
return 0;
88+
}
89+
}
7090
```
7191

7292
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int countStudents(int[] students, int[] sandwiches) {
3+
int[] counter = new int[2];
4+
for (int i : students) {
5+
counter[i] += 1;
6+
}
7+
for (int i = 0; i < sandwiches.length; ++i) {
8+
if (counter[sandwiches[i]] == 0) {
9+
return sandwiches.length - i;
10+
}
11+
counter[sandwiches[i]] -= 1;
12+
}
13+
return 0;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def countStudents(self, students: List[int], sandwiches: List[int]) -> int:
3+
counter = collections.Counter(students)
4+
for i, sandwich in enumerate(sandwiches):
5+
if counter[sandwich] == 0:
6+
return len(students) - i
7+
counter[sandwich] -= 1
8+
return 0

solution/1800-1899/1870.Minimum Speed to Arrive on Time/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868

6969
二分法。
7070

71+
以“二分法”方式枚举速度值,找到满足条件的最小速度。
72+
7173
<!-- tabs:start -->
7274

7375
### **Python3**
@@ -104,7 +106,7 @@ class Solution {
104106
if (dist.length - 1 >= hour) {
105107
return -1;
106108
}
107-
int l = 0, r = 10000000;
109+
int l = 1, r = 10000000;
108110
while (l < r) {
109111
int m = (l + r) >> 1;
110112
if (arriveOnTime(dist, m, hour)) {

solution/1800-1899/1870.Minimum Speed to Arrive on Time/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class Solution {
9494
if (dist.length - 1 >= hour) {
9595
return -1;
9696
}
97-
int l = 0, r = 10000000;
97+
int l = 1, r = 10000000;
9898
while (l < r) {
9999
int m = (l + r) >> 1;
100100
if (arriveOnTime(dist, m, hour)) {

solution/1800-1899/1870.Minimum Speed to Arrive on Time/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ public int minSpeedOnTime(int[] dist, double hour) {
33
if (dist.length - 1 >= hour) {
44
return -1;
55
}
6-
int l = 0, r = 10000000;
6+
int l = 1, r = 10000000;
77
while (l < r) {
88
int m = (l + r) >> 1;
99
if (arriveOnTime(dist, m, hour)) {

0 commit comments

Comments
 (0)