LeetCode 2751. 机器人碰撞 C#
原题链接
困难
作者:
hpstory
,
2023-07-03 13:41:23
,
所有人可见
,
阅读 89
C# 代码
public class Solution {
public IList<int> SurvivedRobotsHealths(int[] positions, int[] healths, string directions) {
int n = positions.Length;
int[] index = Enumerable.Range(0, n).ToArray();
Array.Sort(index, (x, y) => positions[x] - positions[y]);
Stack<int> stack = new Stack<int>();
for (int i = 0; i < n; i++)
{
if (directions[index[i]] == 'R')
{
stack.Push(index[i]);
continue;
}
while (stack.Count > 0)
{
int t = stack.Peek();
if (healths[t] < healths[index[i]])
{
healths[stack.Pop()] = 0;
healths[index[i]]--;
}
else if (healths[t] == healths[index[i]])
{
healths[stack.Pop()] = 0;
healths[index[i]] = 0;
break;
}
else
{
healths[t]--;
healths[index[i]] = 0;
if (healths[t] == 0)
{
stack.Pop();
}
break;
}
}
}
List<int> result = new List<int>();
for (int i = 0; i < n; i++)
{
if (healths[i] > 0) result.Add(healths[i]);
}
return result;
}
}