-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
59 lines (59 loc) · 1.71 KB
/
Solution.cs
File metadata and controls
59 lines (59 loc) · 1.71 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class Solution
{
public string PushDominoes(string dominoes)
{
// n = 10^5
int n = dominoes.Length;
char[] ret = dominoes.ToCharArray();
(char, int)[] prefixState = new (char, int)[n];
prefixState[0] = ('.', -1);
for (int i = 1; i < n; i++)
{
if (ret[i - 1] != '.')
{
prefixState[i] = (ret[i - 1], i - 1);
}
else
{
prefixState[i] = prefixState[i - 1];
}
}
(char, int)[] suffixState = new (char, int)[n];
suffixState[n - 1] = ('.', n);
for (int i = n - 2; i >= 0; i--)
{
if (ret[i + 1] != '.')
{
suffixState[i] = (ret[i + 1], i + 1);
}
else
{
suffixState[i] = suffixState[i + 1];
}
}
for (int i = 0; i < n; i++)
{
if (ret[i] != '.') continue;
var left = prefixState[i];
var right = suffixState[i];
int diff1 = Math.Abs(left.Item2 - i);
int diff2 = Math.Abs(right.Item2 - i);
if ((left.Item1 == 'L' || left.Item1 == '.') && (right.Item1 == 'R' || right.Item1 == '.'))
{
// do nothing
}
else if (left.Item1 == 'R' && right.Item1 == 'L')
{
if (diff1 == diff2)
{
// do nothing
}
else if (diff1 > diff2) ret[i] = 'L';
else ret[i] = 'R';
}
else if (left.Item1 == 'R') ret[i] = 'R';
else ret[i] = 'L';
}
return new(ret);
}
}