1094 a sort problem
This commit is contained in:
		
							
								
								
									
										47
									
								
								1094-20231203-pass/main.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								1094-20231203-pass/main.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | ||||
| #include<stdcpp.h> | ||||
| using namespace std; | ||||
| class Solution { | ||||
| public: | ||||
|     bool carPooling(vector<vector<int>>& trips, int capacity) { | ||||
|         int len = trips.size(); | ||||
|         struct op{ | ||||
|             bool tp; | ||||
|             int people; | ||||
|             int station; | ||||
|         }; | ||||
|         vector<op> ops(trips.size()*2+10); | ||||
|         for(int i = 0 ; i < trips.size() ;i++){ | ||||
|             op in = {true,trips[i][0],trips[i][1]}; | ||||
|             op out = {false,trips[i][0],trips[i][2]}; | ||||
|             ops[i*2] = in; | ||||
|             ops[i*2+1] = out; | ||||
|         } | ||||
|         sort(ops.begin(), ops.end(), [](const op a, const op& b){ | ||||
|             return a.station<b.station||(a.station == b.station && a.tp < b.tp); | ||||
|         }); | ||||
|         int peo = 0; | ||||
|         for(int i = 0 ; i < ops.size() ; i++){ | ||||
|             if(!ops[i].tp){ | ||||
|                 peo-=ops[i].people; | ||||
|             } | ||||
|             if(ops[i].tp){ | ||||
|                 peo+=ops[i].people; | ||||
|                 if(peo>capacity) | ||||
|                     return false; | ||||
|             } | ||||
|         } | ||||
|         return true; | ||||
|     } | ||||
| }; | ||||
|  | ||||
| int main(){ | ||||
|     Solution sol; | ||||
|     vector<int> sta1 = {2,1,5}; | ||||
|     vector<int> sta2 = {3,3,7}; | ||||
|     vector<vector<int> >ex1 = {sta1,sta2}; | ||||
|     cout<<sol.carPooling(ex1, 4)<<endl; | ||||
|     cout<<sol.carPooling(ex1, 5)<<endl; | ||||
|      | ||||
|      | ||||
|     return 0; | ||||
| } | ||||
		Reference in New Issue
	
	Block a user