Re: [闲聊] 每日leetcode

楼主: Rushia (みけねこ的鼻屎)   2024-07-13 10:57:03
https://leetcode.com/problems/robot-collisions/
2751. Robot Collisions
给你三个长度一样的阵列positions, healths, directions,分别表示机器人的座标、
生命、行走方向,机器人同时开始行走,如果不同方向的机器人碰到了,分成两情况:
1.其中一个机器人生命较高,生命较低的机器人hp变0,生命较高的机器人生命减一
2.机器人生命一样,两个hp一起变0
返回剩余hp大于0的机器人生命值阵列,并且按照原始输入的顺序。
Example:
https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png
Input: positions = [3,5,2,6], healths = [10,10,15,12], directions = "RLRL"
Output: [14]
Explanation: There are 2 collisions in this example. Firstly, robot 1 and
robot 2 will collide, and since both have the same health, they will be
removed from the line. Next, robot 3 and robot 4 will collide and since robot
4's health is smaller, it gets removed, and robot 3's health becomes 15 - 1 =
14. Only robot 3 remains, so we return [14].
思路:
1.首先,因为positions不是一定照顺序,所以我们先把他排序,从左到右处理。
2.使用一个stack储存往右的机器人编号,如果下次遇到往左的机器人就一直相撞
直到其中一个方向的机器人生命为0。
3.遍历healths阵列,按照顺序把生命大于0的值存起来就好。
java code:
作者: loserforever (请大家帮我抓小偷)   2023-07-13 10:57:00
周末还要卷喔
作者: ImpotenCat (阳痿猫猫)   2024-07-13 11:00:00
躺平了
作者: smart0eddie (smart0eddie)   2024-07-13 11:13:00
ㄉㄕ

Links booklink

Contact Us: admin [ a t ] ucptt.com