Skip to content

Commit dcdd3f2

Browse files
committed
https://leetcode.cn/problems/number-of-students-unable-to-eat-lunch/
1 parent da27934 commit dcdd3f2

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ https://leetcode.cn/problems/count-number-of-distinct-integers-after-reverse-ope
7171

7272
https://leetcode.cn/problems/possible-bipartition/
7373

74+
https://leetcode.cn/problems/number-of-students-unable-to-eat-lunch/
75+
7476
https://leetcode.cn/problems/construct-binary-search-tree-from-preorder-traversal/
7577

7678
https://leetcode.cn/problems/fruit-into-baskets/

construct-binary-search-tree-from-preorder-traversal/test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Deno.test("construct-binary-search-tree-from-preorder-traversal", () => {
1212
[
1313
[8, 5, 10, 1, 7, null, 12],
1414
[1, null, 3],
15-
].map(TreeNodeLeetCodeFromJSON)
15+
].map(TreeNodeLeetCodeFromJSON),
1616
);
1717
assertEquals(
1818
[
@@ -24,6 +24,6 @@ Deno.test("construct-binary-search-tree-from-preorder-traversal", () => {
2424
[
2525
[8, 5, 10, 1, 7, null, 12],
2626
[1, null, 3],
27-
]
27+
],
2828
);
2929
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export default function countStudents(
2+
students: number[],
3+
sandwiches: number[],
4+
): number {
5+
const cnt = Object.assign([0, 0], countBy(students));
6+
7+
while (true) {
8+
if (students.length === 0) return 0;
9+
10+
if (cnt[sandwiches[0]] === 0) {
11+
return students.length;
12+
}
13+
if (students[0] === sandwiches[0]) {
14+
cnt[students[0]]--;
15+
students.shift();
16+
sandwiches.shift();
17+
} else {
18+
students.push(students[0]);
19+
students.shift();
20+
}
21+
}
22+
}
23+
import { countBy } from "../deps.ts";

0 commit comments

Comments
 (0)