2 Star 0 Fork 0

xinanXu/myleetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
LC1094.cpp 1015 Bytes
一键复制 编辑 原始数据 按行查看 历史
xinanXu 提交于 2023-12-02 13:32 . 1094. 拼车
bool cmp(const std::vector<int> &a, const std::vector<int> &b) {
return a[1] < b[1];
}
class Solution {
public:
bool carPooling(vector<vector<int>>& trips, int capacity) {
// Analog
sort(trips.begin(), trips.end(), cmp);
int len = trips.size();
if (len == 0) return true;
vector<int> seats(capacity, 0);
int f = 0, nowSeats = 0;
for (int i = 0; i < len; i++) {
for (int j = 0; j < capacity; j++) {
if (seats[j] && seats[j] <= trips[i][1])
seats[j] = 0, nowSeats--;
}
if (nowSeats + trips[i][0] <= capacity) {
int j = 0, t = nowSeats;
while (nowSeats - t < trips[i][0]) {
if (seats[j] == 0) {
seats[j] = trips[i][2];
nowSeats++;
}
j++;
}
}
else return false;
}
return true;
}
};
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/DearAtri/myleetcode.git
git@gitee.com:DearAtri/myleetcode.git
DearAtri
myleetcode
myleetcode
master

搜索帮助