2100.cpp (1090B)
1 class Solution { 2 public: 3 vector<int> goodDaysToRobBank(const vector<int> &security, int time) const { 4 static int up[100001], down[100001]; 5 const int n = size(security); 6 7 if (time == 0) { 8 vector<int> res(n); 9 iota(begin(res), end(res), 0); 10 return res; 11 } 12 13 up[0] = down[n - 1] = 0; 14 for (int i = 0, cnt = 1; i < n - 1; i++) { 15 if (security[i] >= security[i + 1]) 16 cnt++; 17 else 18 cnt = 0; 19 up[i + 1] = cnt; 20 } 21 22 for (int i = n - 1, cnt = 0; i >= 1; i--) { 23 if (security[i] >= security[i - 1]) 24 cnt++; 25 else 26 cnt = 0; 27 down[i - 1] = cnt; 28 } 29 30 vector<int> res; 31 for (int i = time; i < n - time; i++) { 32 if (up[i] < time || down[i] < time) continue; 33 if (security[i] > security[i - 1]) continue; 34 if (security[i] > security[i + 1]) continue; 35 36 res.emplace_back(i); 37 } 38 39 return res; 40 } 41 };