Jim and the Orders

Sort by

recency

|

539 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

    Python Solution using key-value pairs -

    def jimOrders(orders):
        # Write your code here
        customer_time = {i + 1: sum(orders[i]) for i in range(len(orders))}
        
        sorted_orders = sorted(customer_time, key=lambda x: customer_time[x])
        
        return sorted_orders
    
  • + 0 comments

    Easy cpp approach:-

    vector jimOrders(vector> orders) {

    int n=orders.size();
    
    vector<int>dt(n,0);
    vector<int>cid(n,0);
    
    
    
    for (int i=0;i<n;i++)
    {
        dt[i]= orders[i][1]+orders[i][0];
        cid[i]=i+1;
    }
    
    for (int i=0;i<n-1;i++)
    {
        for (int j=0;j<n-i-1;j++)
        {
            if (dt[j]>dt[j+1])
            {
                swap(dt[j],dt[j+1]);
                swap(cid[j],cid[j+1]);
            }
        }
    }
    
    return cid;
    

    }****

  • + 0 comments

    Here is my solution in PHP :

    function jimOrders($orders) {
        // Write your code here
        $hasil_hitung = [];
        $hasil = [];
        
        foreach ($orders as $key=>$val) {
            $hasil_hitung[$key+1] = $val[0] + $val[1];
        }
        
        asort($hasil_hitung);
    
        foreach ($hasil_hitung as $key=>$val) {
            $hasil[] = $key;
        }
        return $hasil;
    
    }
    
  • + 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;
    }