Drill

ProblemsJava › games

Look up a player level

mediumgamesBinary searchArraysJava

A game tracks experience points against a table of ascending thresholds. Find the highest level reached.

levelProgress(xp: int, thresholds: list<int>) → int

Java needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

int levelProgress(int xp, List<Integer> thresholds) {
    
}

Worked examples

CallResult
levelProgress(50, Main.<Integer>ls(10, 30, 60, 100))1
levelProgress(100, Main.<Integer>ls(10, 30, 60, 100))3
levelProgress(5, Main.<Integer>ls(10, 30, 60))-1
levelProgress(0, Main.<Integer>ls())0

Hint

Walk the thresholds and remember the last one that was met.

Reference solution in Java
int levelProgress(int xp, List<Integer> thresholds) {
    if (thresholds.size() == 0) return 0;
    if (xp < thresholds.get(0)) return -1;
    int result = 0;
    for (int i = 0; i < thresholds.size(); i++) {
        if (thresholds.get(i) <= xp) result = i;
    }
    return result;
}

The same problem in another language

More games problems in Java