Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Complete the maximumSuperiorCharacters function below.
- static long maximumSuperiorCharacters(int[] freq) {
- // number of superior.
- long s = 0;
- // number of non-superior. (l = low)
- long l = 0;
- // add all letters to the set from a to z.
- for (int i = 0; i < freq.length; i++) {
- assert(s == 0 || l > s);
- // number of next letter that is higher than all previous
- long f = freq[i];
- // total number of letters after adding the last one.
- long n = l+s+f;
- // calculate new number of superior:
- if (l == 0) {
- // if current set of letters is empty, they will all be the
- // same, no superiors.
- s = 0;
- } else if (f >= l+s) {
- // if there are more of new letter then all previous, we can
- // put new one between each old and it will be the biggest
- // number of superiors possible
- s = l+s-1;
- } else if (l-s-1 >= f) {
- // there is along sequence of non-superiors on one side of our
- // string, that can be improved by stuffing new letter at every
- // second position.
- s = s + f;
- } else {
- // it's even better, we have enough of new letter to make
- // an optimal string.
- s = (n-1)/2;
- }
- l = n - s ;
- }
- return s;
- }
Advertisement
Add Comment
Please, Sign In to add comment