Problem solution · C++

Design Ride Sharing System

Design Ride Sharing System: a C++ solution using hash-based lookup. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Hash-based lookup
Source
Kamyu LeetCode Solutions
Length
80 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Hash-based lookup

For Design Ride Sharing System, the implementation stores previously seen values or frequencies in a hash table for direct membership and lookup operations.

  1. Decide the key that represents the information needed later.
  2. Update its count or stored state while scanning the input.
  3. Use constant-time expected lookups to detect matches or assemble the result.

Code notes

  • 80 lines of C++ from the credited upstream file design-ride-sharing-system.cpp.
  • The implementation visibly relies on sequence storage, hash lookup.
  • No explicit loop blocks detected.

Complexity

Expected hash operations are constant time, but the surrounding scan and the number of stored keys determine total work and memory.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeDesign Ride Sharing System · C++C++
Use this to learn the idea, then write your own version.
// Time:  ctor:                 O(1)//        addRider:             O(1)//        addDriver:            O(1)//        matchDriverWithRider: O(1)//        cancelRider:          O(1)// Space: O(n) // ordered dictclass RideSharingSystem {public:    RideSharingSystem() {            }        void addRider(int riderId) {        riders_[riderId] = true;    }        void addDriver(int driverId) {        drivers_[driverId] = true;    }        vector<int> matchDriverWithRider() {        if (empty(riders_) || empty(drivers_)) {            return {-1, -1};        }        const auto r = riders_.front().first;        const auto d = drivers_.front().first;        riders_.pop(r);        drivers_.pop(d);        return {d, r};            }        void cancelRider(int riderId) {        if (riders_.count(riderId)) {            riders_.pop(riderId);        }    } private:    template<typename K, typename V>    class OrderedDict {    public:        int count(const K& key) const {            return lookup_.count(key);        }                V& operator[](const K& key) {            if (!lookup_.count(key)) {                list_.emplace_back();                list_.rbegin()->first = key;                lookup_[key] = prev(end(list_));            }            return lookup_[key]->second;        }         pair<K, V> front() const {            return *cbegin(list_);        }                void pop(const K& key) {            if (!lookup_.count(key)) {                return;            }            list_.erase(lookup_[key]);            lookup_.erase(key);        }                bool empty() const {            return list_.empty();        }        private:        list<pair<K, V>> list_;        unordered_map<K, typename list<pair<K, V>>::iterator> lookup_;    };     OrderedDict<int, bool> riders_, drivers_;}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗