-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
42 lines (40 loc) · 1014 Bytes
/
Solution.cs
File metadata and controls
42 lines (40 loc) · 1014 Bytes
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
public class Solution
{
public IList<int> GoodDaysToRobBank(int[] security, int time)
{
int n = security.Length;
int[] prefix = new int[n];
prefix[0] = 0;
for (int i = 1; i < n; i++)
{
if (security[i - 1] >= security[i])
{
prefix[i] = prefix[i - 1];
}
else
{
prefix[i] = i;
}
}
int[] suffix = new int[n];
suffix[n - 1] = n - 1;
for (int i = n - 2; i >= 0; i--)
{
if (security[i] <= security[i + 1])
{
suffix[i] = suffix[i + 1];
}
else
{
suffix[i] = i;
}
}
IList<int> ret = [];
for (int i = 0; i < n; i++)
{
int left = prefix[i], right = suffix[i];
if (Math.Abs(left - i) >= time && Math.Abs(right - i) >= time) ret.Add(i);
}
return ret;
}
}