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
|
class Solution {
vector<int> ans;
public:
void dfsGoodDaysToRobBank(vector<int>& security, int time, int index) {
if (security.size() - index < (time << 1) + 1) {
return ;
}
int i = index;
int sz = time;
for (; sz--; i++) {
if (security[i] < security[i + 1]) {
return dfsGoodDaysToRobBank(security, time, i + 1);
}
}
int may_ans_index = i;
sz = time;
for (; sz--; i++) {
if (security[i] > security[i + 1]) {
// [1,2,5,4,1,0,2,4,5,3,1,2,4,3,2,4,8] 2
// 这个不会输出结果 5 , 只输出 [10, 14], 而不是[5,10,14]
// return dfsGoodDaysToRobBank(security, time, i);
return dfsGoodDaysToRobBank(security, time, i + 1 - time);
}
}
ans.push_back(may_ans_index);
for (int j = may_ans_index; j <= security.size() - time; j++) {
// 前面可知security[j] <= security[j + 1]
if (security[j] == security[j + 1]) {
if (security[j + time] <= security[j + time + 1]) {
ans.push_back(j + 1);
} else {
return dfsGoodDaysToRobBank(security, time, j + time);
}
}
if (security[j] < security[j + 1]) {
return dfsGoodDaysToRobBank(security, time, j + 1);
}
}
}
vector<int> goodDaysToRobBank(vector<int>& security, int time) {
ans.clear();
if (time == 0) {
for (int i = 0; i < security.size(); i++) {
ans.push_back(i);
}
return ans;
}
if (security.size() < (time << 1) + 1) {
return ans;
}
int index = 0;
dfsGoodDaysToRobBank(security, time, index);
return ans;
}
};
|