Skip to content

Commit

Permalink
Create Car Pooling.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayu-99 authored Jan 6, 2022
1 parent d21ee82 commit 1a7abd9
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Leetcode Challenge/January/Car Pooling.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Solution {
public:

// bool carPooling(vector<vector<int>>& trips, int capacity) {

// priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> heap;
// for(int i=0;i<trips.size();++i)
// {
// heap.push({trips[i][1],trips[i][0]});
// heap.push({trips[i][2],-trips[i][0]});
// }
// int onboard =0;
// while(!heap.empty())
// {
// onboard = onboard + heap.top().second;
// heap.pop();
// if(onboard>capacity) return false;
// }
// return true;

// }

bool carPooling(vector<vector<int>>& trips, int capacity) {
int stops[1001] = {};
for (auto t : trips) {
stops[t[1]] += t[0];
stops[t[2]] -= t[0];
}

for (auto i = 0; capacity >= 0 && i < 1001; ++i) {
capacity -= stops[i];
}
return capacity >= 0;q
}


};

0 comments on commit 1a7abd9

Please sign in to comment.