Jim and the Orders

Sort by

recency

|

545 Discussions

|

  • + 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);
        }
    }
    
  • + 0 comments

    unorthodox java solution but i assume very less space complexity

    class Result {

    /*
     * Complete the 'jimOrders' function below.
     *
     * The function is expected to return an INTEGER_ARRAY.
     * The function accepts 2D_INTEGER_ARRAY orders as parameter.
     */
    
    public static List<Integer> jimOrders(List<List<Integer>> orders) {
    // Write your code here
    
    
    long[] val=new long[orders.size()];
    for(int i=0;i<orders.size();i++){
        long temp=orders.get(i).get(0)+orders.get(i).get(1);
        val[i]=temp*10000 +(i+1);
    
    }
    Arrays.sort(val);
    System.out.println(Arrays.toString(val));
    ArrayList<Integer> list=new ArrayList<>();
    for(long i:val){
        list.add((int)(i%10000));
    }
    
    return list;
    }
    

    }