forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
36 lines (27 loc) · 792 Bytes
/
main.cpp
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
/// Source : https://leetcode.com/problems/find-all-good-indices/
/// Author : liuyubobobo
/// Time : 2022-09-26
#include <iostream>
#include <vector>
using namespace std;
/// DP
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
vector<int> goodIndices(vector<int>& nums, int k) {
int n = nums.size();
vector<int> dp1(n, 1), dp2(n, 1);
for(int i = 1; i < n; i ++)
if(nums[i] <= nums[i - 1]) dp1[i] = 1 + dp1[i - 1];
for(int i = n - 2; i >= 0; i --)
if(nums[i] <= nums[i + 1]) dp2[i] = 1 + dp2[i + 1];
vector<int> res;
for(int i = 1; i < n - 1; i ++)
if(dp1[i - 1] >= k && dp2[i + 1] >= k) res.push_back(i);
return res;
}
};
int main() {
return 0;
}