Drill

ProblemsC++ › reporting

Which month grew the most

mediumreportingArraysMathC++

A board slide calls out the month with the largest jump in revenue over the month before it.

biggestJump(points: list<Point>) → string?

C++ 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

std::optional<std::string> biggestJump(std::vector<Point> points) {
    
}

Worked examples

CallResult
biggestJump(std::vector<Point>{Point{std::string("jan"), 100}, Point{std::string("feb"), 150}, Point{std::string("mar"), 160}})std::optional<std::string>(std::string("feb"))
biggestJump(std::vector<Point>{Point{std::string("jan"), 100}, Point{std::string("feb"), 110}, Point{std::string("mar"), 120}})std::optional<std::string>(std::string("feb"))
biggestJump(std::vector<Point>{Point{std::string("jan"), 100}, Point{std::string("feb"), 90}})std::nullopt
biggestJump(std::vector<Point>{Point{std::string("jan"), 100}})std::nullopt

Hint

Start from index 1 and keep the best strictly-greater difference, so ties keep the first one.

Reference solution in C++
std::optional<std::string> biggestJump(std::vector<Point> points) {
    std::optional<string> bestMonth;
    int bestJump = 0;
    for (size_t i = 1; i < points.size(); i++) {
        int jump = points[i].total - points[i - 1].total;
        if (jump > bestJump) { bestJump = jump; bestMonth = points[i].month; }
    }
    return bestMonth;
}

The same problem in another language

More reporting problems in C++