forked from gouthampradhan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubArraysWithBoundedMaximum.java
59 lines (57 loc) · 1.56 KB
/
SubArraysWithBoundedMaximum.java
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package array;
/**
* Created by gouthamvidyapradhan on 29/03/2019 We are given an array A of positive integers, and
* two positive integers L and R (L <= R).
*
* <p>Return the number of (contiguous, non-empty) subarrays such that the value of the maximum
* array element in that subarray is at least L and at most R.
*
* <p>Example : Input: A = [2, 1, 4, 3] L = 2 R = 3 Output: 3 Explanation: There are three subarrays
* that meet the requirements: [2], [2, 1], [3]. Note:
*
* <p>L, R and A[i] will be an integer in the range [0, 10^9]. The length of A will be in the range
* of [1, 50000].
*/
public class SubArraysWithBoundedMaximum {
/**
* Main method
*
* @param args
*/
public static void main(String[] args) {
int[] A = {2, 1, 4, 3};
System.out.println(new SubArraysWithBoundedMaximum().numSubarrayBoundedMax(A, 2, 3));
}
public int numSubarrayBoundedMax(int[] A, int L, int R) {
int[] DP = new int[A.length];
int v = -1;
for (int i = A.length - 1; i >= 0; i--) {
if (A[i] >= L && A[i] <= R) {
if (v != -1) {
DP[i] = v - i + 1;
} else {
DP[i] = 1;
v = i;
}
} else if (A[i] < L) {
if (v == -1) {
v = i;
}
if (i + 1 < A.length) {
if (A[i + 1] < L || (A[i + 1] >= L && A[i + 1] <= R)) {
DP[i] = DP[i + 1];
} else {
DP[i] = 0;
}
}
} else {
v = -1;
}
}
int sum = 0;
for (int i = 0; i < DP.length; i++) {
sum += DP[i];
}
return sum;
}
}