-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_1764_canChoose.cc
74 lines (65 loc) · 1.38 KB
/
Problem_1764_canChoose.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
bool check(vector<int> &groups, vector<int> &nums, int k)
{
if (k + groups.size() > nums.size())
{
return false;
}
for (int i = 0; i < groups.size(); i++)
{
if (groups[i] != nums[k + i])
{
return false;
}
}
return true;
}
bool canChoose(vector<vector<int>> &groups, vector<int> &nums)
{
int n = groups.size();
int m = nums.size();
int j = 0;
for (int i = 0; i < m && j < n;)
{
if (check(groups[j], nums, i))
{
i += groups[j].size();
j++;
}
else
{
i++;
}
}
return j == n;
}
// TODO: KMP
};
void testCanChoose()
{
Solution s;
vector<vector<int>> g1 = {{1, -1, -1}, {3, -2, 0}};
vector<int> n1 = {1, -1, 0, 1, -1, -1, 3, -2, 0};
vector<vector<int>> g2 = {{10, -2}, {1, 2, 3, 4}};
vector<int> n2 = {1, 2, 3, 4, 10, -2};
vector<vector<int>> g3 = {{1, 2, 3}, {3, 4}};
vector<int> n3 = {7, 7, 1, 2, 3, 4, 7, 7};
vector<vector<int>> g4 = {{21, 22, 21, 22, 21, 30}};
vector<int> n4 = {21, 22, 21, 22, 21, 22, 21, 30};
EXPECT_TRUE(s.canChoose(g1, n1));
EXPECT_FALSE(s.canChoose(g2, n2));
EXPECT_FALSE(s.canChoose(g3, n3));
EXPECT_TRUE(s.canChoose(g4, n4));
EXPECT_SUMMARY;
}
int main()
{
testCanChoose();
return 0;
}