[python] Count Collisions on a Road leetcode 2211

Designated Hitter Jack·2023년 10월 30일
0

leetcode

목록 보기
7/11
post-thumbnail

문제

  1. Count Collisions on a Road

User Accepted:4524
User Tried:6754
Total Accepted:4633
Total Submissions:15467
Difficulty:Medium

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 <= 105
  directions[i] is either 'L', 'R', or 'S'.

풀이

class Solution:
    def countCollisions(self, directions: str) -> int:
        collision = 0
        #방향이 R인 차량이 연속으로 나오는 경우 앞에서 충돌이 발생하면 연쇄적으로 추가로 충돌함
        multi_collide = 0
        directions_list = list(directions)
        
        for i in range(len(directions_list) - 1):
            if directions_list[i] == 'R' and directions_list[i + 1] == 'L':
                collision += 2
                collision = collision + multi_collide
                multi_collide = 0
                directions_list[i + 1] = 'S'
            elif directions_list[i] == 'R' and directions_list[i + 1] == 'S':
                collision += 1
                collision = collision + multi_collide
                multi_collide = 0
            elif directions_list[i] == 'R' and directions_list[i + 1] == 'R':
                multi_collide += 1
            elif directions_list[i] == 'S' and directions_list[i + 1] == 'L':
                collision += 1
                directions_list[i + 1] = 'S'
            
        return collision

문제를 잘 읽고 문제에서 제시하는 대로 조건을 만들면 되는 문제인데, 문제에서 제시한대로만 조건을 짜면 R이 연속해서 나오는 경우를 놓치게 된다.
즉, RRRRRRRRRRRL이라는 문자열을 만나면 실제로는 R + L에 더해 뒤에 나오는 R의 갯수만큼 충돌이 추가로 발생하지만 이를 놓치기 쉽다.
이를 막기 위해 multi_collide라는 변수에 R이 몇 번이나 나왔는지를 기록한 다음, R이 충돌한다면 충돌 횟수에 multi_collide를 더해주는 식으로 코드를 짜야 한다.
그리고 충돌이 일어나서 멈춘 L 방향 차량을 S로 바꾸어줘야 S + L인 경우에 발생하는 충돌을 빼먹지 않고 셀 수 있다.

profile
Fear always springs from ignorance.

0개의 댓글