PaulGraphov

Untitled

Apr 29th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1.     // Complete the maximumSuperiorCharacters function below.
  2.     static long maximumSuperiorCharacters(int[] freq) {
  3.         // number of superior.
  4.         long s = 0;
  5.         // number of non-superior. (l = low)
  6.         long l = 0;
  7.         // add all letters to the set from a to z.
  8.         for (int i = 0; i < freq.length; i++) {
  9.             assert(s == 0 || l > s);
  10.             // number of next letter that is higher than all previous
  11.             long f = freq[i];
  12.             // total number of letters after adding the last one.
  13.             long n = l+s+f;
  14.             // calculate new number of superior:
  15.             if (l == 0) {
  16.                 // if current set of letters is empty, they will all be the
  17.                 // same, no superiors.
  18.                 s = 0;
  19.             } else if (f >= l+s) {
  20.                 // if there are more of new letter then all previous, we can
  21.                 // put new one between each old and it will be the biggest
  22.                 // number of superiors possible
  23.                 s = l+s-1;
  24.             } else if (l-s-1 >= f) {
  25.                 // there is along sequence of non-superiors on one side of our
  26.                 // string, that can be improved by stuffing new letter at every
  27.                 // second position.
  28.                 s = s + f;
  29.             } else {
  30.                 // it's even better, we have enough of new letter to make
  31.                 // an optimal string.
  32.                 s = (n-1)/2;
  33.             }
  34.             l = n - s ;
  35.         }
  36.        
  37.         return s;
  38.     }
Advertisement
Add Comment
Please, Sign In to add comment