zhin's blog

By zhin, history, 4 hours ago, In English

Question How to solve this ?

  • Vote: I like it
  • -4
  • Vote: I do not like it

»
2 hours ago, # |
  Vote: I like it +1 Vote: I do not like it

I'm pretty sure coding a greedy is worth a shot. If $$$x = y$$$, the answer is just $$$\frac{nx}{2}$$$; if $$$x < y$$$, remove all identical characters you can, then you are left with at most $$$26$$$ different characters, and you just remove those using the $$$y$$$ operation; if $$$x > y$$$, store character counts in a heap or a SortedList and always use $$$y$$$ on the max and second max frequency characters. If there is only $$$1$$$ distinct character left, use $$$x$$$ on it.

  • »
    »
    95 minutes ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    For the $$$x>y$$$ condition, a better method would be to just check if character having the max frequency has the frequency greater than sum of frequency of all the other characters , then the answer would be $$$x*\frac{(2*(max freq) - length of string)}{2} + y*((length of string)-(max freq))$$$ and other wise, it would be possible to cancel out all of the characters with some other character and it would be end up being $$$ \frac{ny}{2} $$$.

    • »
      »
      »
      89 minutes ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      That is a good point. It kind of reminds me of this problem.