-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2751-Robot-Collisions.ts
56 lines (49 loc) · 1.51 KB
/
2751-Robot-Collisions.ts
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
interface Robot {
position: number
health: number
direction: string
index: number
}
function survivedRobotsHealths(positions: number[], healths: number[], directions: string): number[] {
const robots: Robot[] = positions.map((pos, idx) => ({
position: pos,
health: healths[idx],
direction: directions[idx],
index: idx,
}))
robots.sort((a, b) => a.position - b.position)
const stack: Robot[] = []
const survived: Set<number> = new Set()
for (const robot of robots) {
if (robot.direction === 'R') {
stack.push(robot)
} else {
while (stack.length > 0 && robot.health > 0) {
const top = stack[stack.length - 1]
if (top.health > robot.health) {
top.health -= 1
robot.health = 0
} else if (top.health < robot.health) {
robot.health -= 1
stack.pop()
} else {
stack.pop()
robot.health = 0
}
}
if (robot.health > 0) {
survived.add(robot.index)
}
}
}
for (const robot of stack) {
survived.add(robot.index)
}
const result: number[] = new Array(positions.length).fill(0)
for (const robot of robots) {
if (survived.has(robot.index)) {
result[robot.index] = robot.health
}
}
return result.filter(health => health > 0)
};