Jim and the Orders

Sort by

recency

|

546 Discussions

|

  • + 0 comments

    Easy Approach by adding the values and assigning the index of each order to retrive based on their priority and then sorting the result based on the 2nd element in the summed array by using lambda function and then iterating through that sorted array in order to retrieve the order priority list and then returning it in type of res. Hope you understand!! Happy Coding!!

    def jimOrders(l): sum_l = sorted([[sum(j),i+1] for i,j in enumerate(l)], key = lambda x: x[0]) res = [ i[1] for i in sum_l] return res `

  • + 0 comments

    Here is my c++ solution, you can watch the explanation video here : https://youtu.be/ISg5mFFafws

    vector<int> jimOrders(vector<vector<int>> orders) {
        vector<vector<int>> temp;
        for(int i = 0; i < orders.size(); i++){
            int deliver = orders[i][0] + orders[i][1];
            temp.push_back({deliver, i+1});
        }
        sort(temp.begin(), temp.end());
        vector<int> result;
        for(auto element: temp)result.push_back(element[1]);
        
        return result;
    }
    
  • + 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());
        }
    
  • + 0 comments

    I got it in 2 lines!

    def jimOrders(orders):
        orders = sorted([[orders[i][0] + orders[i][1] + 1, i + 1] for i in range(len(orders))])
        return [str(order[1]) for order in orders]
    
  • + 0 comments
    import java.io.*;
    import java.util.*;
    import java.lang.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            List<AbstractMap.SimpleEntry<Integer,Integer>> entryList = new LinkedList<>();
            int n = input.nextInt();
            
            //Builds a list of key value pairs
            for(int i = 0; i < n; i++)
            {
                entryList.add(new AbstractMap.SimpleEntry<Integer,Integer>(i+1,input.nextInt() + input.nextInt()));
            }        
            
            //Sorts the list of entries according to value
            Collections.sort(entryList, (p1,p2) -> (p1.getValue()).compareTo(p2.getValue()));
            
            //Print the sorted list of entries
            StringBuilder output = new StringBuilder("");
            for (AbstractMap.SimpleEntry<Integer,Integer> entry : entryList) {
                output.append(entry.getKey() + " ");
            }
            System.out.println(output);
        }
    }