Leetcode 練習:2022-01-06

January 06, 2022

Car Pooling

題號:1094

var carPooling = function(trips, capacity) {
  const arr = []
  for (const [numPassengers, from, to] of trips) {
    for (var i = from; i < to; i++) {
      if ((arr[i] || 0) + numPassengers > capacity) {
        return false
      }
      
      arr[i] = (arr[i] || 0) + numPassengers
    }
  }
  return true
}
var carPooling = function(trips, capacity) {
  const arr = []
  for (const [numPassengers, from, to] of trips) {
    for (var i = from; i < to; i++) {
      if ((arr[i] || 0) + numPassengers > capacity) {
        return false
      }
      
      arr[i] = (arr[i] || 0) + numPassengers
    }
  }
  return true
}

Profile picture

Written by Wei Hung who is thinking. You should follow them on Twitter