-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2211-CountCollisionsOnARoad.go
More file actions
100 lines (86 loc) · 4.29 KB
/
2211-CountCollisionsOnARoad.go
File metadata and controls
100 lines (86 loc) · 4.29 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
package main
// 2211. Count Collisions on a Road
// There are n cars on an infinitely long road.
// The cars are numbered from 0 to n - 1 from left to right and each car is present at a unique point.
// You are given a 0-indexed string directions of length n.
// directions[i] can be either 'L', 'R', or 'S' denoting whether the ith car is moving towards the left, towards the right, or staying at its current point respectively.
// Each moving car has the same speed.
// The number of collisions can be calculated as follows:
// When two cars moving in opposite directions collide with each other, the number of collisions increases by 2.
// When a moving car collides with a stationary car, the number of collisions increases by 1.
// After a collision, the cars involved can no longer move and will stay at the point where they collided.
// Other than that, cars cannot change their state or direction of motion.
// Return the total number of collisions that will happen on the road.
// Example 1:
// Input: directions = "RLRSLL"
// Output: 5
// Explanation:
// The collisions that will happen on the road are:
// - Cars 0 and 1 will collide with each other. Since they are moving in opposite directions, the number of collisions becomes 0 + 2 = 2.
// - Cars 2 and 3 will collide with each other. Since car 3 is stationary, the number of collisions becomes 2 + 1 = 3.
// - Cars 3 and 4 will collide with each other. Since car 3 is stationary, the number of collisions becomes 3 + 1 = 4.
// - Cars 4 and 5 will collide with each other. After car 4 collides with car 3, it will stay at the point of collision and get hit by car 5. The number of collisions becomes 4 + 1 = 5.
// Thus, the total number of collisions that will happen on the road is 5.
// Example 2:
// Input: directions = "LLRR"
// Output: 0
// Explanation:
// No cars will collide with each other. Thus, the total number of collisions that will happen on the road is 0.
// Constraints:
// 1 <= directions.length <= 10^5
// directions[i] is either 'L', 'R', or 'S'.
import "fmt"
import "strings"
func countCollisions(directions string) int {
res, rcount, scount := 0, 0, 0
for _, v := range directions {
if v == 'R' {
rcount++
continue
}
if v == 'L' && rcount + scount == 0 { continue } // no collision
if v == 'L' { // SL
res++
}
res += rcount // RS
rcount = 0
scount |= 1
}
return res
}
func countCollisions1(dir string) int {
dir = strings.TrimLeft(dir, "L")
dir = strings.TrimRight(dir, "R")
return len(dir) - strings.Count(dir, "S")
}
func main() {
// Example 1:
// Input: directions = "RLRSLL"
// Output: 5
// Explanation:
// The collisions that will happen on the road are:
// - Cars 0 and 1 will collide with each other. Since they are moving in opposite directions, the number of collisions becomes 0 + 2 = 2.
// - Cars 2 and 3 will collide with each other. Since car 3 is stationary, the number of collisions becomes 2 + 1 = 3.
// - Cars 3 and 4 will collide with each other. Since car 3 is stationary, the number of collisions becomes 3 + 1 = 4.
// - Cars 4 and 5 will collide with each other. After car 4 collides with car 3, it will stay at the point of collision and get hit by car 5. The number of collisions becomes 4 + 1 = 5.
// Thus, the total number of collisions that will happen on the road is 5.
fmt.Println(countCollisions("RLRSLL")) // 5
// Example 2:
// Input: directions = "LLRR"
// Output: 0
// Explanation:
// No cars will collide with each other. Thus, the total number of collisions that will happen on the road is 0.
fmt.Println(countCollisions("LLRR")) // 0
fmt.Println(countCollisions("LLLLLLLLL")) // 0
fmt.Println(countCollisions("RRRRRRRRR")) // 0
fmt.Println(countCollisions("SSSSSSSSS")) // 0
fmt.Println(countCollisions("LLLRRRSS")) // 3
fmt.Println(countCollisions("LRSLRSLRS")) // 5
fmt.Println(countCollisions1("RLRSLL")) // 5
fmt.Println(countCollisions1("LLRR")) // 0
fmt.Println(countCollisions1("LLLLLLLLL")) // 0
fmt.Println(countCollisions1("RRRRRRRRR")) // 0
fmt.Println(countCollisions1("SSSSSSSSS")) // 0
fmt.Println(countCollisions1("LLLRRRSS")) // 3
fmt.Println(countCollisions1("LRSLRSLRS")) // 5
}