Skip to content

Commit cd4db86

Browse files
authored
Create 838.Push-Dominoes.cpp
1 parent ef2cc0a commit cd4db86

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using PII = pair<int,int>;
2+
class Solution {
3+
public:
4+
string pushDominoes(string dominoes)
5+
{
6+
int n = dominoes.size();
7+
vector<int>rets(n, -2);
8+
9+
queue<PII>q;
10+
for (int i=0; i<n; i++)
11+
{
12+
if (dominoes[i]=='L')
13+
{
14+
q.push({i, -1});
15+
rets[i] = -1;
16+
}
17+
else if (dominoes[i]=='R')
18+
{
19+
q.push({i, 1});
20+
rets[i] = 1;
21+
}
22+
}
23+
24+
while (!q.empty())
25+
{
26+
int len = q.size();
27+
unordered_map<int,int>Map;
28+
while (len--)
29+
{
30+
auto [pos, dir] = q.front();
31+
q.pop();
32+
33+
if (dir==1 && pos+1<n && rets[pos+1]==-2)
34+
Map[pos+1] += 1;
35+
if (dir==-1 && pos-1>=0 && rets[pos-1]==-2)
36+
Map[pos-1] -= 1;
37+
}
38+
for (auto x: Map)
39+
{
40+
q.push(x);
41+
rets[x.first] = x.second;
42+
}
43+
}
44+
45+
string ans;
46+
for (int i=0; i<n; i++)
47+
{
48+
if (rets[i]==1)
49+
ans.push_back('R');
50+
else if (rets[i]==-1)
51+
ans.push_back('L');
52+
else
53+
ans.push_back('.');
54+
}
55+
return ans;
56+
}
57+
};

0 commit comments

Comments
 (0)