CodeWars - Metro  

by ne on 2022-02-16 under Algo/DS/Problems tagged with codewars

Hi,
The problem description is in the comments above the solution below.

The problem belongs to CodeWars, you can try out it yourself on their platform.


/**
 *
 * There is a bus moving in the city, and it takes and drop some people in each bus stop.
 *
 * You are provided with a list (or array) of integer pairs. Elements of each pair represent number of people get into bus (The first item) and number of people get off the bus (The second item) in a bus stop.
 *
 * Your task is to return number of people who are still in the bus after the last bus station (after the last array). Even though it is the last bus stop, the bus is not empty and some people are still in the bus, and they are probably sleeping there :D
 *
 * Take a look on the test cases.
 *
 *   ArrayList list = new ArrayList();
 *     list.add(new int[] {10,0});
 *     list.add(new int[] {3,5});
 *     list.add(new int[] {2,5});
 *     assertEquals(5, metro.countPassengers(list));
 *
 * Please keep in mind that the test cases ensure that the number of people in the bus is always >= 0. So the return integer can't be negative.
 *
 * The second value in the first integer array is 0, since the bus is empty in the first bus stop.
 *
 */
import java.util.ArrayList;
class Metro {
    public static int countPassengers(ArrayList stops) {
        return stops.stream().reduce((a, b) -> {
            return new int[]{a[0] + b[0] - a[1] - b[1], 0};
        }).get()[0];
    }
}