-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path838.push-dominoes.cpp
102 lines (100 loc) · 2.81 KB
/
838.push-dominoes.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* @lc app=leetcode id=838 lang=cpp
*
* [838] Push Dominoes
*
* https://leetcode.com/problems/push-dominoes/description/
*
* algorithms
* Medium (44.76%)
* Likes: 335
* Dislikes: 37
* Total Accepted: 13.5K
* Total Submissions: 30.1K
* Testcase Example: '".L.R...LR..L.."'
*
* There are N dominoes in a line, and we place each domino vertically
* upright.
*
* In the beginning, we simultaneously push some of the dominoes either to the
* left or to the right.
*
*
*
* After each second, each domino that is falling to the left pushes the
* adjacent domino on the left.
*
* Similarly, the dominoes falling to the right push their adjacent dominoes
* standing on the right.
*
* When a vertical domino has dominoes falling on it from both sides, it stays
* still due to the balance of the forces.
*
* For the purposes of this question, we will consider that a falling domino
* expends no additional force to a falling or already fallen domino.
*
* Given a string "S" representing the initial state. S[i] = 'L', if the i-th
* domino has been pushed to the left; S[i] = 'R', if the i-th domino has been
* pushed to the right; S[i] = '.', if the i-th domino has not been pushed.
*
* Return a string representing the final state.
*
* Example 1:
*
*
* Input: ".L.R...LR..L.."
* Output: "LL.RR.LLRRLL.."
*
*
* Example 2:
*
*
* Input: "RR.L"
* Output: "RR.L"
* Explanation: The first domino expends no additional force on the second
* domino.
*
*
* Note:
*
*
* 0 <= N <= 10^5
* String dominoes contains only 'L', 'R' and '.'
*
*
*/
class Solution {
public:
string pushDominoes(string dominoes) {
vector<int> left(dominoes.length(),-1), right(dominoes.length(),-1);
string ans;
for(int i=0; i<dominoes.length(); i++){
if(dominoes[i]=='.'&&i>0){
right[i] = right[i-1];
}else if (dominoes[i]=='R')
{
right[i] = i;
}
}
for (int i=dominoes.length()-1; i>=0; i--){
if(dominoes[i]=='.'&&i<(dominoes.length()-1)){
left[i] = left[i+1];
}else if(dominoes[i]=='L'){
left[i] = i;
}
}
for (int i=0; i<dominoes.length(); i++){
if((left[i]==-1&&right[i]==-1)||(left[i]!=-1&&right[i]!=-1&&abs(i-left[i])==abs(i-right[i])))
ans.push_back('.');
else if(left[i]==-1)
ans.push_back('R');
else if(right[i]==-1)
ans.push_back('L');
else if(abs(i-right[i])<abs(i-left[i]))
ans.push_back('R');
else
ans.push_back('L');
}
return ans;
}
};