Saturday, May 16, 2015

LevelUp

Problem:

TopCoder Problem Statement - LevelUp
Single Round Match 170 Round 1 - Division II, Level One

Overview:

Determine the number of experience point a character needs in order to reach the next level.

Java Source:
     1 public class LevelUp {
     2
     3  public int toNextLevel(int[] expNeeded, int received) {
     4
     5   int i = 0;
     6
     7         /*
     8         * Find the first expNeeded element that is greater
     9         * than received.
    10         */
    11         while (expNeeded[i] <= received) i++;
    12
    13         /*
    14         * Return the difference between the next level, and
    15         * the amount received.
    16         */
    17         return (expNeeded[i] - received);
    18
    19  }
    20 }
Notes:

Certainly one of the easiest problems to solve. Just loop through the values in expNeeded until you find one that is greater than received. Then just return that next value minus the amount of received.

Really the only thing to be careful of is the case where the amount received exactly equals the amount needed for a level. In this case, return the amount needed to reach the next level, and not 0.

No comments:

Post a Comment