Jim and the Orders

Sort by

recency

|

536 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

    C#

    public static List<int> jimOrders(List<List<int>> orders)
    {
        var res= new List<int>();
        Dictionary<int,int> map = new Dictionary<int,int>();
    
        for (int i = 0; i < orders.Count;i++)
        {
            map[i+1] = orders[i][0] + orders[i][1];
    
        }
    
        var mapSort = map.OrderBy(x => x.Value).ToList();
        foreach (var item in mapSort)
        {
            res.Add(item.Key);
        }
        return res;
    }
    
  • + 0 comments

    vector jimOrders(vector> orders) { vector> sum; int i = 1;

    for(auto it : orders){
        int order = it[0];
        int prep = it[1];
        sum.push_back({order + prep, i});
        i++;
    }
    sort(sum.begin(),sum.end());
    
    vector<int> ans;
    
    for(auto it : sum){
        ans.push_back(it.second);
    }
    
    return ans;
    

    }

  • + 2 comments

    whats wrong in this code? (2 test cases failed)

    public static List jimOrders(List> orders) { // Write your code here

          List<Integer>list = new ArrayList<>();
          for(int i=0;i<orders.size();i++){
             list.add(orders.get(i).get(0)+orders.get(i).get(1)); 
           }
        List<Integer>list2 = new ArrayList<>(list);
        Collections.sort(list2);
        List<Integer>ans = new ArrayList<>();
        for(int i=0;i<list2.size();i++){
           ans.add(list.indexOf(list2.get(i))+1); 
         }
        return ans;
    }
    
  • + 0 comments

    Java8

    public static List<Integer> jimOrders(List<List<Integer>> orders) {
    // Write your code here
    List<Integer> list=new ArrayList<>();
    Map<Integer,Integer> map=new HashMap<>();
    int x;
    for(int i=0;i<orders.size();i++){
        x=orders.get(i).get(0)+orders.get(i).get(1);
        map.put(i+1, x);
    }
    
       //map is sorted by values
      final Map<Integer, Integer> sortedByTime = map.entrySet()
                .stream()
                .sorted(Map.Entry.comparingByValue())
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new)); 
    
    
     for(int i : sortedByTime.keySet()){
         list.add(i);
     }
    

    return list; }