Car Pooling

A car with capacity empty seats drives east only: it never turns around.

You are given trips, where trips[i] = [numPassengers, from, to] means a group of numPassengers people gets in at kilometre from and gets out at kilometre to. At any stop, people getting out leave before new people get in, so a group that gets out at kilometre 5 never shares the car with a group that gets in at kilometre 5.

Return true if the car can serve every trip without ever carrying more than capacity passengers, and false otherwise. The trips can be given in any order.

Constraints

  • 1 <= trips.length <= 1000
  • trips[i].length == 3
  • 1 <= numPassengers <= 100
  • 0 <= from < to <= 1000
  • 1 <= capacity <= 10^5

Examples

Example 1

Input: trips = [[3,2,6],[2,4,8]], capacity = 4
Output: false

Between stops 4 and 6 both groups are in the car: 3 + 2 = 5 passengers, more than 4 seats.

Example 2

Input: trips = [[3,2,6],[2,4,8]], capacity = 5
Output: true

The peak is 5 passengers, which fits exactly.

Example 3

Input: trips = [[2,1,4],[3,4,6]], capacity = 3
Output: true

The first group gets out at stop 4 before the second group gets in, so there are never more than 3 passengers.

JavaScript you'll use: Arrays: create, read, update

/**
 * @param {number[][]} trips
 * @param {number} capacity
 * @return {boolean}
 */
function carPooling(trips, capacity) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.