-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution838.cs
43 lines (37 loc) · 1.07 KB
/
Solution838.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution838
{
/// <summary>
/// 838. Push Dominoes - Medium
/// <a href="https://leetcode.com/problems/push-dominoes">See the problem</a>
/// </summary>
public string PushDominoes(string dominoes)
{
var n = dominoes.Length;
var forces = new int[n];
var force = 0;
for (var i = 0; i < n; i++)
{
if (dominoes[i] == 'R') force = n;
else if (dominoes[i] == 'L') force = 0;
else force = Math.Max(force - 1, 0);
forces[i] += force;
}
force = 0;
for (var i = n - 1; i >= 0; i--)
{
if (dominoes[i] == 'L') force = n;
else if (dominoes[i] == 'R') force = 0;
else force = Math.Max(force - 1, 0);
forces[i] -= force;
}
var sb = new StringBuilder();
foreach (var f in forces)
{
sb.Append(f > 0 ? 'R' : f < 0 ? 'L' : '.');
}
return sb.ToString();
}
}