-
Notifications
You must be signed in to change notification settings - Fork 11
/
PoisonousPlants.java
54 lines (48 loc) Β· 1.7 KB
/
PoisonousPlants.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
// https://www.hackerrank.com/challenges/poisonous-plants/problem
package stacks;
import helper.Input;
import java.util.Stack;
public class PoisonousPlants {
public static void main(String[] args) {
int[] plants = Input.getArray();
System.out.println(daysToSafety(plants));
}
private static int daysToSafety(int[] plants) {
int minimumDays = 0;
Stack<Info> stack = new Stack<>();
stack.push(new Info(plants[0], 0, false, 0));
for (int index = 1, days = 0 ; index < plants.length ; index++) {
if (plants[index] > stack.peek().element) {
stack.push(new Info(plants[index], index, true, days + 1));
minimumDays = Math.max(minimumDays, days + 1);
days = 0;
} else {
if (!stack.peek().removable) {
stack.push(new Info(plants[index], index, false, 0));
days = 0;
} else {
int barrier = stack.peek().days;
while (stack.peek().removable && stack.peek().days <= barrier) {
Info info = stack.pop();
days = info.days;
minimumDays = Math.max(minimumDays, days);
}
index--;
}
}
}
return minimumDays;
}
private static class Info {
int element;
int index;
boolean removable;
int days;
Info(int element, int index, boolean removable, int days) {
this.element = element;
this.index = index;
this.removable = removable;
this.days = days;
}
}
}