add: 220306-CN [cpp]

This commit is contained in:
Lam Haoyin 2022-03-06 01:26:12 +08:00
parent e7dba79fa8
commit 1516e81415
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 25 additions and 1 deletions

24
cpp/2203/220306-CN.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <vector>
#include <iostream>
class Solution {
public:
static std::vector<int> goodDaysToRobBank(const std::vector<int>& security, int time) {
int n = security.size();
std::vector<int> ret, isNotIncreasingSince(n), isNotDecreasingSince(n);
for (int i = 1; i < n; ++i) {
isNotDecreasingSince[i] = (security[i - 1] <= security[i]) ? isNotDecreasingSince[i - 1] : i;
isNotIncreasingSince[i] = (security[i - 1] >= security[i]) ? isNotIncreasingSince[i - 1] : i;
}
for (int i = time; i < n - time; ++i)
if (isNotIncreasingSince[i] <= i - time && isNotDecreasingSince[i + time] <= i)
ret.push_back(i);
return ret;
}
};
int main() {
auto ret = Solution::goodDaysToRobBank({1,2,3,4,5,6},2);
for (int i : ret)
std::cout << i << ' ';
}

View File

@ -3,4 +3,4 @@ PROJECT(2203)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2203 220305.cpp) ADD_EXECUTABLE(2203 220306-CN.cpp)