leetcode

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

2767.cpp (605B)


      1 class Solution {
      2     int rec(const char *s, const int n, int i) const {
      3         if (i == n) return 0;
      4         if (s[i] == '0') return -1;
      5 
      6         int res = INT_MAX, crnt = 0;
      7         for (; i < n; i++) {
      8             crnt = (crnt << 1) | (s[i] & 1);
      9             if (15625 % crnt == 0) {
     10                 const int next = rec(s, n, i + 1);
     11                 if (next == -1) continue;
     12                 res = min(res, 1 + next);
     13             }
     14         }
     15 
     16         return res != INT_MAX ? res : -1;
     17     }
     18 
     19   public:
     20     int minimumBeautifulSubstrings(const string &s) const { return rec(s.data(), size(s), 0); }
     21 };