leetcode

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

1781.cpp (635B)


      1 class Solution {
      2   public:
      3     int beautySum(const string &s) {
      4         int res = 0;
      5         for (auto i = 0; i < s.size(); i++) {
      6             int cnt[27] = {0}, maxi = 0, mini = 0;
      7             for (auto j = i; j < s.size(); j++) {
      8                 const int idx = s[j] & 0x1F;
      9                 maxi = max(maxi, ++cnt[idx]);
     10                 if (mini >= cnt[idx] - 1) {
     11                     mini = cnt[idx];
     12                     for (int k = 1; k <= 26; ++k)
     13                         mini = min(mini, cnt[k] == 0 ? INT_MAX : cnt[k]);
     14                 }
     15                 res += maxi - mini;
     16             }
     17         }
     18         return res;
     19     }
     20 };