Jim and the Orders

  • + 0 comments

    My Java solution with o(n log n) time and o(n) space:

    public static List<Integer> jimOrders(List<List<Integer>> orders) {
            List<int[]> indexedOrders = new ArrayList<>(); //holds order idxs and their times
    
            //get the times for each order idx
            for (int i = 0; i < orders.size(); i++) {
                int time = orders.get(i).get(0) + orders.get(i).get(1);
                indexedOrders.add(new int[]{i + 1, time});
            }
            
            //sort orders ascending to earliest orders
            indexedOrders.sort(Comparator.comparingInt(o -> o[1]));
            
            //return list of only the order idxs
            return indexedOrders.stream()
                    .map(o -> o[0])
                    .collect(Collectors.toList());
        }