Skip to content

Commit 71d1e22

Browse files
committed
Day 51 - getting more greedy
1 parent acb2284 commit 71d1e22

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
| Current Status| Stats |
66
| :------------: | :----------: |
7-
| Total Problems | 70 |
8-
| Current Streak | 50 |
9-
| Longest Streak | 50 ( August 17, 2015 - October 5, 2015 ) |
7+
| Total Problems | 71 |
8+
| Current Streak | 51 |
9+
| Longest Streak | 51 ( August 17, 2015 - October 6, 2015 ) |
1010

1111
</center>
1212

@@ -134,3 +134,4 @@ Include contains single header implementation of data structures and some algori
134134
| Problem | Solution |
135135
| :------------ | :----------: |
136136
| Given two integer arrays, A and B, each containing N integers. You are free to permute the order of the elements in the arrays.Is there an permutation A', B' possible of A and B, such that, A'<sub>i</sub>+B'<sub>i</sub> ≥ K for all i, where A'<sub>i<sub> denotes the i<sup>th<sup> element in the array A' and B'<sub>i</sub> denotes i<sup>th</sup> element in the array B'.| [two_arrays.cpp](greedy_problems/two_arrays.cpp)|
137+
|John is taking orders. The i<sup>th</sup> order is placed by the i<sup>th</sup> customer at t<sub>i</sub> time and it takes d<sub>i</sub> time to procees. What is the order in which the customers will get their orders? (see more details in solutions's comments)|[orders_order.cpp](greedy_problems/orders_order.cpp)|

greedy_problems/orders_order.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* John is taking orders.
3+
* The ith order is placed by the ith customer at ti time and it takes di time to procees.
4+
* What is the order in which the customers will get their orders?
5+
*
6+
* Input
7+
* On the first line you will get n, the number of orders.
8+
* Then n lines will follow. On the (i+1)th line, you will get ti and di separated by a single space.
9+
*
10+
* Output
11+
* Print the order ( as single space separated integers ) in which the burger customers get their orders.
12+
* If two customers get the order at the same time,
13+
* then print the smallest numbered order first.(remember, the customers are numbered 1 to n).
14+
*
15+
* Approach :
16+
* By greedy approach, we can conclude the customer who ordered first and his order duration is smaller will get his order first.
17+
* So we will sort the orders based on time at which order was taken, + duration it will take to fulfill that order.
18+
*/
19+
20+
#include <iostream>
21+
#include <algorithm>
22+
using namespace std;
23+
24+
struct order {
25+
int index;
26+
int time;
27+
int duration;
28+
};
29+
30+
bool compareFunc(order & o1, order & o2) {
31+
if ( (o1.time + o1.duration) < (o2.time + o2.duration) ) {
32+
return true;
33+
} else if ( o1.time + o1.duration == o2.time + o2.duration ) {
34+
if (o1.index < o2.index) {
35+
return true;
36+
}
37+
}
38+
return false;
39+
}
40+
41+
int main() {
42+
int N;
43+
cin >> N ;
44+
order * orders = new order[N];
45+
for (int i = 0; i < N; ++i) {
46+
orders[i].index = i+1;
47+
cin >> orders[i].time;
48+
cin >> orders[i].duration;
49+
}
50+
sort(orders, orders+N, compareFunc);
51+
for( int i = 0; i < N; ++i) {
52+
std::cout << orders[i].index << " ";
53+
}
54+
std::cout << std::endl;
55+
return 0;
56+
}
57+

0 commit comments

Comments
 (0)