五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

LeetCode 2211. Count Collisions on a Road

2023-03-25 13:14 作者:您是打尖兒還是住店呢  | 我要投稿

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'.

  • 這個是過了代碼,先說說不過的代碼,將RL的變成SS,然后就是去判斷每個位置最近的S在哪里,L的判斷左邊的S,R的判斷右邊的S,然后就Time Limit Exceed了。

  • 仔細想想,直接先將RL的變成SS,然后找到最左端的S的位置,跟最右端S的位置,只要是L的位置小于最左端S的位置,R的位置小于最右端S的位置,那么一定會碰撞的。然后就過了。

Runtime:?41 ms, faster than?50.00%?of?Java?online submissions for?Count Collisions on a Road.

Memory Usage:?44.2 MB, less than?15.85%?of?Java?online submissions for?Count Collisions on a Road.


LeetCode 2211. Count Collisions on a Road的評論 (共 條)

分享到微博請遵守國家法律
高邮市| 安西县| 武强县| 玛曲县| 望城县| 聂拉木县| 安达市| 涟水县| 红河县| 页游| 泾源县| 龙游县| 永定县| 永清县| 甘孜县| 牟定县| 陕西省| 中阳县| 乌拉特后旗| 始兴县| 高雄市| 交口县| 河北省| 定陶县| 永新县| 团风县| 巴南区| 喀喇沁旗| 天长市| 高邑县| 通山县| 东源县| 博客| 彭泽县| 瓦房店市| 威信县| 张掖市| 泾川县| 亳州市| 梁平县| 定襄县|