- Prepare
- Algorithms
- Greedy
- Jim and the Orders
- Discussions
Jim and the Orders
Jim and the Orders
+ 0 comments JAVA
public static List<Integer> jimOrders(List<List<Integer>> orders) { Map<Integer,Integer> sums=new HashMap<>(); for(int i=0;i<orders.size();i++){ sums.put(i,orders.get(i).get(0)+orders.get(i).get(1)); } List<Map.Entry<Integer,Integer>> listSums= sums.entrySet().stream(). sorted((Comparator.comparingInt(Map.Entry::getValue))) .collect(Collectors.toList()); return listSums.stream().map(elem->elem.getKey()+1).collect(Collectors.toList()); }
+ 0 comments C# solution (single-statement version that avoids the need for auxillary array or extra storage):
public static List<int> jimOrders(List<List<int>> orders) { return orders .Select((o, idx) => (idx + 1, o[0] + o[1])) .OrderBy(o => o, Comparer<(int, int)>.Create((a, b) => a.Item2 == b.Item2 ? (a.Item1 + a.Item2).CompareTo(b.Item1 + b.Item2) : a.Item2.CompareTo(b.Item2))) .Select(o => o.Item1) .ToList(); }
+ 0 comments C# solution:
var result = orders .Select((o, idx) => (idx + 1, o[0] + o[1])) .ToList(); result.Sort((o1, o2) => { if (o1.Item2 == o2.Item2) { return (o1.Item1 + o1.Item2).CompareTo(o2.Item1 + o2.Item2); } else { return o1.Item2.CompareTo(o2.Item2); } }); return result.Select(r => r.Item1).ToList();
+ 0 comments def jimOrders(orders): return [z[2] for z in sorted([(orders[i][0], orders[i][1], i + 1) for i in range(len(orders))], key=lambda y: y[0] + y[1])]
+ 0 comments 1.Create an empty list called serveTimes to store serve time and customer number pairs.
2.Iterate over each order in the orders list:
a.Get the order number and preparation time.
b.Calculate the serve time by adding the order number and preparation time.
c.Add a new array with serve time and customer number to the serveTimes list.
3.Sort the serveTimes list in ascending order based on serve time and customer number.
4.Create an empty list called deliveryOrder.
5.Iterate over each serve time array in the serveTimes list: Get the customer number from each serve time array and add it to the deliveryOrder list.
6.Return the deliveryOrder list as the result.
Sort 502 Discussions, By:
Please Login in order to post a comment