Блог пользователя _humblefool_

Автор _humblefool_, история, 4 года назад, По-английски

Problem link I did not find any better solution in the discuss section of leetcode that's why posting here.

  • Проголосовать: нравится
  • -21
  • Проголосовать: не нравится

»
4 года назад, скрыть # |
 
Проголосовать: нравится -30 Проголосовать: не нравится

It is very disappointed to see that useless blog gets upvotes and a lot of people comment on those blogs but I even did not got any answer of my genuine problem. I was thinking that there are so many genius coders that will answer my problem. Shame on you CodeForces community.

  • »
    »
    4 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Sometimes the comunity is obviously being biased, but let not just focus on the vote since it is not that important, compared to the important of current knowledge — which can be easily comprehend through the color.

»
4 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

What's the need of it? You can write simple O(n^3) solution.

»
4 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I don't think I can solve it

»
4 года назад, скрыть # |
Rev. 4  
Проголосовать: нравится 0 Проголосовать: не нравится

By quick thinking, $$$O(n^2 + alphabet)$$$ is possible if you iterating each letter and increase its frequency while maintaining the maximum and the minimum

int n = s.size();
long long res = 0;
for (int i = 0; i < n; ++i)
{
    for (int j = i; j < n; ++j)
    {
        ++cnt[s[j]];
        update(max); <-- you can just maximize(max, ++cnt[s[j]]); at the beginning
        update(min); <-- this can be a bit tricky to get O(1), but O(log alphabet) coding is simpler
        res += max - min;
    }

    for (int j = i; j < n; ++j)
        --cnt[s[j]];
}