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