-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjim_and_the_orders.cpp
More file actions
34 lines (31 loc) · 874 Bytes
/
jim_and_the_orders.cpp
File metadata and controls
34 lines (31 loc) · 874 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//this is listed as Easy but i wanted to include
//since i love greedy solutions.
//also this is 40p solution.
#include <bits/stdc++.h>
using namespace std;
vector<int> jimOrders(vector<vector<int>> orders) {
map<int, std::vector<int>> serve_times; //<--- using map (or multimap) is the trick
vector<int> result;
for (int i = 0; i < orders.size(); i++)
{
int t = orders[i][0] + orders[i][1];
auto it = serve_times.find(t);
if (it != serve_times.end())
{
it->second.push_back(i + 1);
}
else
{
serve_times[t] = vector<int>();
serve_times[t].push_back(i + 1);
}
}
for (auto pair : serve_times)
{
for (int i = 0; i < pair.second.size(); i++)
{
result.push_back(pair.second[i]);
}
}
return result;
}