feat:[1094] 拼车

This commit is contained in:
Wen 2023-06-30 14:17:51 +08:00
parent d23cc5785a
commit f0950ff295
1 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,42 @@
/*
* @lc app=leetcode.cn id=1094 lang=javascript
*
* [1094] 拼车
*/
// @lc code=start
/**
* @param {number[][]} trips
* @param {number} capacity
* @return {boolean}
*/
var carPooling = function (trips, capacity) {
//下车队列
trips.sort((o1, o2) => o1[1] - o2[1]);
const getout = [];
let cur = 0;
for (let i = 0; i < trips.length; i++) {
//当前到达的位置
const distance = trips[i][1];
//上车人数
const getOnNums = trips[i][0];
//目标位置
const targetDistance = trips[i][2];
for (let j = 0; j < getout.length; j++) {
if (getout[j][0] <= distance) {
cur -= getout[j][1];
getout.splice(j, 1);
j--;
} else {
continue;
}
}
cur += getOnNums;
if (cur > capacity) {
return false;
}
getout.push([targetDistance, getOnNums]);
}
return true;
};
// @lc code=end