leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0838.cpp (829B)
0 class Solution { 1 public: 2 string pushDominoes(const string &dominoes) const { 3 static int forces[100001]; 4 5 const int n = size(dominoes); 6 string res(n, '.'); 7 8 for (int i = 0, force = 0; i < n; i++) { 9 if (dominoes[i] == 'R') 10 force = n; 11 else if (dominoes[i] == 'L') 12 force = 0; 13 else 14 force = max(force - 1, 0); 15 forces[i] = force; 16 } 17 18 for (int i = n - 1, force = 0; i >= 0; i--) { 19 if (dominoes[i] == 'L') 20 force = n; 21 else if (dominoes[i] == 'R') 22 force = 0; 23 else 24 force = max(force - 1, 0); 25 if (forces[i] != force) res[i] = forces[i] > force ? 'R' : 'L'; 26 } 27 28 return res; 29 } 30 };