Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8af24eb

Browse files
solves poisonous plants
1 parent b5f2f4b commit 8af24eb

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

β€Žhackerrank-data-structures.imlβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<module type="JAVA_MODULE" version="4">
3+
<component name="FacetManager">
4+
<facet type="Python" name="Python">
5+
<configuration sdkName="Python 3.7" />
6+
</facet>
7+
</component>
38
<component name="NewModuleRootManager" inherit-compiler-output="true">
49
<exclude-output />
510
<content url="file://$MODULE_DIR$">
611
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
712
</content>
813
<orderEntry type="inheritedJdk" />
914
<orderEntry type="sourceFolder" forTests="false" />
15+
<orderEntry type="library" exported="" name="Python 3.7 interpreter library" level="application" />
1016
</component>
1117
</module>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// https://www.hackerrank.com/challenges/poisonous-plants/problem
2+
3+
package stacks;
4+
5+
import helper.Input;
6+
import java.util.Stack;
7+
8+
public class PoisonousPlants {
9+
public static void main(String[] args) {
10+
int[] plants = Input.getArray();
11+
System.out.println(daysToSafety(plants));
12+
}
13+
14+
private static int daysToSafety(int[] plants) {
15+
int minimumDays = 0;
16+
Stack<Info> stack = new Stack<>();
17+
stack.push(new Info(plants[0], 0, false, 0));
18+
for (int index = 1, days = 0 ; index < plants.length ; index++) {
19+
if (plants[index] > stack.peek().element) {
20+
stack.push(new Info(plants[index], index, true, days + 1));
21+
minimumDays = Math.max(minimumDays, days + 1);
22+
days = 0;
23+
} else {
24+
if (!stack.peek().removable) {
25+
stack.push(new Info(plants[index], index, false, 0));
26+
days = 0;
27+
} else {
28+
int barrier = stack.peek().days;
29+
while (stack.peek().removable && stack.peek().days <= barrier) {
30+
Info info = stack.pop();
31+
days = info.days;
32+
minimumDays = Math.max(minimumDays, days);
33+
}
34+
index--;
35+
}
36+
}
37+
}
38+
return minimumDays;
39+
}
40+
41+
private static class Info {
42+
int element;
43+
int index;
44+
boolean removable;
45+
int days;
46+
47+
Info(int element, int index, boolean removable, int days) {
48+
this.element = element;
49+
this.index = index;
50+
this.removable = removable;
51+
this.days = days;
52+
}
53+
}
54+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Input 00
2+
7
3+
6 5 8 4 7 10 9
4+
Output 00
5+
2
6+
7+
Input 7
8+
8
9+
3 1 10 7 3 5 6 6
10+
Output 7
11+
3
12+
13+
Input 29
14+
4
15+
3 2 5 4
16+
Output 29
17+
2
18+
19+
20+
Input 30
21+
7
22+
4 3 7 5 6 4 2
23+
Output 30
24+
3

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /