- Decide the key that represents the information needed later.
- Update its count or stored state while scanning the input.
- Use constant-time expected lookups to detect matches or assemble the result.
Code notes
- 49 lines of C++ from the credited upstream file design-order-management-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.
Use this to learn the idea, then write your own version.
1234567 89class OrderManagementSystem {10public:11 OrderManagementSystem() {12 13 }14 15 void addOrder(int orderId, string orderType, int price) {16 orders_[orderId] = {orderType, price, size(type_price_[orderType][price])};17 type_price_[orderType][price].emplace_back(orderId);18 }19 20 void modifyOrder(int orderId, int newPrice) {21 const auto orderType = get<0>(orders_[orderId]);22 cancelOrder(orderId);23 addOrder(orderId, orderType, newPrice);24 }25 26 void cancelOrder(int orderId) {27 const auto [orderType, price, i] = move(orders_[orderId]);28 auto& arr = type_price_[orderType][price];29 get<2>(orders_[arr.back()]) = i;30 swap(arr[i], arr.back());31 arr.pop_back();32 if (empty(type_price_[orderType][price])) {33 type_price_[orderType].erase(price);34 if (empty(type_price_[orderType])) {35 type_price_.erase(orderType);36 }37 }38 orders_.erase(orderId);39 }40 41 vector<int> getOrdersAtPrice(string orderType, int price) {42 return type_price_.count(orderType) && type_price_[orderType].count(price) ? type_price_[orderType][price] : vector<int>();43 }44 45private:46 unordered_map<int, tuple<string, int, int>> orders_;47 unordered_map<string, unordered_map<int, vector<int>>> type_price_;48};49