# Leetcode 1094. Car Pooling ###### tags: `Leetcode(C++)` 題目 : https://leetcode.com/problems/car-pooling/ 。 想法 : 建立一個陣列,代表車上的狀況,並掃描所有乘客的上站下站資訊,加到陣列裡,如果有一站爆掉就立刻中止回傳答案。 時間複雜度 : O((to-from)*numpassenger)。 程式碼 : ``` class Solution { public: bool carPooling(vector<vector<int>>& trips, int capacity) { int l=trips.size(), check[1010]={0}, ok=1; for(int i=0 ; i<l&&ok ; i++){ int people=trips[i][0]; int _from=trips[i][1]; int _to=trips[i][2]; for(int j=_from ; j<_to&&ok ; j++){ check[j]+=people; if(check[j] > capacity) ok=0; } } if(ok) return true; return false; } }; ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up