-
Notifications
You must be signed in to change notification settings - Fork 2
/
Solution.java
64 lines (47 loc) · 1.44 KB
/
Solution.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package hackrank.algorithm.greedy.jimorder;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
/**
* Jim and the Orders Challenge
*
* @see https://www.hackerrank.com/challenges/jim-and-the-orders
*/
public class Solution {
public static void main(String[] args) {
List<Order> orders = readInput(System.in);
Collections.sort(orders);
System.out.println(orders.stream().map(o -> String.valueOf(o.id)).collect(Collectors.joining(" ")));
}
public static List<Order> readInput(InputStream input) {
Scanner scanner = new Scanner(input);
int numberOrders = scanner.nextInt();
List<Order> orders = new ArrayList<>(numberOrders);
for (int orderId = 1; orderId <= numberOrders; orderId++) {
orders.add(new Order(orderId, scanner.nextInt(), scanner.nextInt()));
}
scanner.close();
return orders;
}
}
class Order implements Comparable<Order> {
int id;
int start;
int cost;
Order(int id, int start, int cost) {
this.id = id;
this.start = start;
this.cost = cost;
}
public int compareTo(Order another) {
int diff = (start + cost) - (another.start + another.cost);
if (diff == 0) {
return id - another.id;
} else {
return diff;
}
}
}