HackerRank 'Jim and the Orders' Solution

by Srikant Padala on March 20, 2016, 4:04 am


Explanation


Sort by finish time
Jim and the Orders Problem Statement

Video

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
 
struct order {
    int id, start, end;
};
 
bool comp(order &o1, order &o2) {
    return (o1.end < o2.end || (o1.end == o2.end && o1.id < o2.id));
}
int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int n,d;
    cin >> n;
    vector<order> orders(n);
    for(int i=0; i < n; i++) {
        cin >> orders[i].start;
        cin >> d;
        orders[i].end = orders[i].start + d;
        orders[i].id = i+1;
    }
    sort(orders.begin(), orders.end(), comp);
    for(int i=0; i < n; i++)
        cout << orders[i].id << " ";
    return 0;
}

Coming Soon.