-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprblm2751.java
More file actions
141 lines (121 loc) · 4.51 KB
/
prblm2751.java
File metadata and controls
141 lines (121 loc) · 4.51 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import java.util.*;
class Robot{
int position;
int health;
char direction;
public Robot(int position, int health, char direction){
this.position = position;
this.health = health;
this.direction = direction;
}
}
public class prblm2751 {
public static List<Integer> survivedRobotsHealths(int[] positions, int[] healths, String directions) {
List<Robot> robots = new ArrayList<>();
for (int i = 0; i < positions.length; i++) {
robots.add(new Robot(positions[i], healths[i], directions.charAt(i)));
}
robots.sort(Comparator.comparingInt(robot -> robot.position));
Stack<Robot> stack = new Stack<>();
for (Robot robot : robots) {
boolean isDied = false;
while (!stack.isEmpty() && stack.peek().direction == 'R' && robot.direction == 'L') {
Robot lastRobot = stack.pop();
if (lastRobot.health > robot.health) {
lastRobot.health -= 1;
stack.push(lastRobot);
isDied = true;
break;
} else if (lastRobot.health == robot.health) {
isDied = true;
break;
}else{
robot.health -= 1;
}
}
if (isDied == false) {
stack.push(robot);
}
}
List<Integer> remainingRobotsHealth = new ArrayList<>();
Map<Integer,Integer> map = new HashMap<>();
while (!stack.isEmpty()) {
Robot robot = stack.pop();
map.put(robot.position, robot.health);
}
for (int pos : positions) {
if(map.containsKey(pos)){
remainingRobotsHealth.add(map.get(pos));
}
}
return remainingRobotsHealth;
}
public static void main(String[] args) {
int[] positions = {5,4,3,2,1};
int[] healths = {2,17,9,15,10};
String directions = "RRRRR";
List<Integer> answer = survivedRobotsHealths(positions, healths, directions);
System.out.println(answer.toString());
}
}
// Solution 2
// import java.util.*;
// class Robot{
// int position;
// int health;
// char direction;
// int idx;
// public Robot(int position, int health, char direction,int idx){
// this.position = position;
// this.health = health;
// this.direction = direction;
// this.idx = idx;
// }
// }
// public class prblm2751 {
// public List<Integer> survivedRobotsHealths(int[] positions, int[] healths, String directions) {
// List<Robot> robots = new ArrayList<>();
// for (int i = 0; i < positions.length; i++) {
// robots.add(new Robot(positions[i], healths[i], directions.charAt(i), i));
// }
// robots.sort(Comparator.comparingInt(robot -> robot.position));
// Stack<Robot> stack = new Stack<>();
// for (Robot robot : robots) {
// boolean isDied = false;
// while (!stack.isEmpty() && stack.peek().direction == 'R' && robot.direction == 'L') {
// Robot lastRobot = stack.pop();
// if (lastRobot.health > robot.health) {
// lastRobot.health -= 1;
// stack.push(lastRobot);
// isDied = true;
// break;
// } else if (lastRobot.health == robot.health) {
// isDied = true;
// break;
// }else{
// robot.health -= 1;
// }
// }
// if (isDied == false) {
// stack.push(robot);
// }
// }
// List<Robot> aliveRobots = new ArrayList<>();
// while (!stack.isEmpty()) {
// aliveRobots.add(stack.pop());
// }
// aliveRobots.sort(Comparator.comparingInt(robot -> robot.idx));
// List<Integer> remainingRobotsHealth = new ArrayList<>();
// for (Robot robot : aliveRobots) {
// remainingRobotsHealth.add(robot.health);
// }
// return remainingRobotsHealth;
// }
// public static void main(String[] args) {
// int[] positions = {5,4,3,2,1};
// int[] healths = {2,17,9,15,10};
// String directions = "RRRRR";
// List<Integer> answer = survivedRobotsHealths(positions, healths, directions);
// System.out.println(answer.toString());
// }
// }