File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments