-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
33 lines (32 loc) · 861 Bytes
/
Program.cs
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
using System;
using System.Linq;
namespace NumberOfSubarraysWithBoundedMaximum
{
class Program
{
static void Main(string[] args)
{
int[] A = { 7, 3, 6, 7, 1 };
int L = 1, R = 4;
Console.WriteLine(NumSubarrayBoundedMax(A, L, R));
}
static int NumSubarrayBoundedMax(int[] A, int L, int R)
{
var dp = new int[A.Length];
var pre = -1;
for (int i = 0; i < dp.Length; i++)
{
if (A[i] < L && i > 0)
dp[i] = dp[i - 1];
else if (L <= A[i] && A[i] <= R)
dp[i] = i - pre;
else if (A[i] > R)
{
dp[i] = 0;
pre = i;
}
}
return dp.Sum();
}
}
}