leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0838.cpp (829B)


      1 class Solution {
      2   public:
      3     string pushDominoes(const string &dominoes) const {
      4         static int forces[100001];
      5 
      6         const int n = size(dominoes);
      7         string res(n, '.');
      8 
      9         for (int i = 0, force = 0; i < n; i++) {
     10             if (dominoes[i] == 'R')
     11                 force = n;
     12             else if (dominoes[i] == 'L')
     13                 force = 0;
     14             else
     15                 force = max(force - 1, 0);
     16             forces[i] = force;
     17         }
     18 
     19         for (int i = n - 1, force = 0; i >= 0; i--) {
     20             if (dominoes[i] == 'L')
     21                 force = n;
     22             else if (dominoes[i] == 'R')
     23                 force = 0;
     24             else
     25                 force = max(force - 1, 0);
     26             if (forces[i] != force) res[i] = forces[i] > force ? 'R' : 'L';
     27         }
     28 
     29         return res;
     30     }
     31 };