Jim's Burgers has a line of hungry customers. Orders vary in the time it takes to prepare them. Determine the order the customers receive their orders. Start by numbering each of the customers from 1 to n, front of the line to the back. You will then be given an order number and a preparation time for each customer.
The time of delivery is calculated as the sum of the order number and the preparation time. If two orders are delivered at the same time, assume they are delivered in ascending customer number order.
For example, there are n= 5 customers in line. They each receive an order number and a preparation time .:
Customer 1 2 3 4 5
Order # 8 5 6 2 4
Prep time 3 6 2 3 3
Calculate:
Serve time 11 11 8 5 7
We see that the orders are delivered to customers in the following order:
Order by:
Serve time 5 7 8 11 11
Customer 4 5 3 1 2
Example:
Input: n = 5, orders = {{8, 1}, {4, 2}, {5, 6}, {3, 1}, {4, 3}}
Output: 4 2 5 1 3
Approach
C+
#include <bits/stdc++.h>using namespace std;vector<int> jimOrders(vector<vector<int>> orders){multimap<int, int> m;int n = orders.size();for (int i = 1; i <= n; i++){int a = orders[i - 1][0], b = orders[i - 1][1];m.insert(make_pair(a + b, i));}vector<int> res;for (auto me = m.begin(); me != m.end(); me++)res.push_back(me->second);return res;}int main(){int n = 5;vector<vector<int>> orders = {{8, 1},{4, 2},{5, 6},{3, 1},{4, 3}};vector<int> result = jimOrders(orders);for (int i = 0; i < result.size(); i++){cout << result[i];if (i != result.size() - 1){cout << " ";}}cout << "\n";return 0;}
No comments:
Post a Comment