-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem_1700_countStudents.cc
56 lines (49 loc) · 1.01 KB
/
Problem_1700_countStudents.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <numeric> // std::accumulate
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
int countStudents(vector<int> &students, vector<int> &sandwiches)
{
// 1的数量
int a = std::accumulate(students.begin(), students.end(), 0); // 累加初始值为0
// 0的数量
int b = students.size() - a;
for (int i = 0; i < sandwiches.size(); i++)
{
if (sandwiches[i] == 0 && b > 0)
{
b--;
}
else if (sandwiches[i] == 1 && a > 0)
{
a--;
}
else
{
// sandwiches是模拟栈
break;
}
}
return b + a;
}
};
void testCountStudents()
{
Solution s;
vector<int> st1 = {1, 1, 0, 0};
vector<int> sa1 = {0, 1, 0, 1};
vector<int> st2 = {1, 1, 1, 0, 0, 1};
vector<int> sa2 = {1, 0, 0, 0, 1, 1};
EXPECT_EQ_INT(0, s.countStudents(st1, sa1));
EXPECT_EQ_INT(3, s.countStudents(st2, sa2));
EXPECT_SUMMARY;
}
int main()
{
testCountStudents();
return 0;
}