-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgooddays.java
87 lines (66 loc) · 2.08 KB
/
gooddays.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.util.ArrayList;
import java.util.List;
public class gooddays {
public static void main(String[] args) {
int security[] = { 5, 3, 3, 3, 5, 6, 2 }, time = 2;
System.out.println(goodDaysToRobBank(security, time));
}
// public static List<Integer> goodDaysToRobBank(int[] security, int time) {
// int l = security.length - 1;
// List<Integer> ans = new ArrayList<>();
// if (l < time)
// return ans;
// for (int i = time; i < security.length-time; i++) {
// if(check(security,i,time)){
// ans.add(i);
// }
// }
// return ans;
// }
// private static boolean check(int[] security, int i, int time) {
// boolean left=true,right=true;
// for(int l=0;l<time;l++){
// if(security[i-l]>security[i-l-1])
// left=false;
// }
// for (int j = 0; j < time; j++) {
// if(security[i+j]>security[i+j+1])
// right= false;
// }
// return left&&right;
// }
public static List<Integer> goodDaysToRobBank(int[] security, int time)
{
int n=security.length;
List<Integer> ans=new ArrayList<>();
int increasing[]=new int[n];
ascending(increasing,security);
int decreasing[]=new int[n];
descending(decreasing,security);
for(int i=0;i<n;i++)
{
if(decreasing[i]>=time && increasing[i]>=time)
ans.add(i);
}
return ans;
}
public static void descending(int decreasing[],int a[])
{
decreasing[0]=0;
for(int i=1;i<a.length;i++)
{
if(a[i]<=a[i-1])
decreasing[i]=decreasing[i-1]+1;
}
}
public static void ascending(int increasing[],int a[])
{
int n=a.length;
increasing[n-1]=0;
for(int i=n-2;i>=0;i--)
{
if(a[i]<=a[i+1])
increasing[i]=increasing[i+1]+1;
}
}
}