Jim and the Orders

Sort by

recency

|

542 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

    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;
    }
    

    }

  • + 0 comments

    Java:

    public static List<Integer> jimOrders(List<List<Integer>> orders) {
      int n = orders.size();
      List<List<Integer>> delivery = new ArrayList<>();
      List<Integer> customers = new ArrayList<>();
    
      for (int i = 0; i < n; i++) {
        int orderNumber = orders.get(i).get(0);
        int prepTime = orders.get(i).get(1);
        int deliverytime = orderNumber + prepTime;
        delivery.add(Arrays.asList(i + 1, deliverytime));
      }
      Collections.sort(delivery, new Comparator<List<Integer>>() {
        @Override
        public int compare(List<Integer> o1, List<Integer> o2) {
          if (o1.get(1) != o2.get(1)) {
            return o1.get(1).compareTo(o2.get(1));
          } else {
            return o1.get(0).compareTo(o2.get(0));
          }
        }
      });
      for (List<Integer> l : delivery) {
        customers.add(l.get(0));
      }
      System.out.println(customers);
      return customers;
    }
    
  • + 0 comments

    I am starting a new journey, and doing coding challenges in ZigLang. IAW the rules of this platform, I will not post the solution in the editorial.

    const std = @import("std");
    const expectEqualSlices = std.testing.expectEqualSlices;
    
    const CompareOrder = struct {
        fn compare(_: void, lhs: [2]i32, rhs: [2]i32) bool {
            if (lhs[0] < rhs[0]) return true;
            if (lhs[0] > rhs[0]) return false;
            return lhs[1] < rhs[1];
        }
    };
    pub fn jimOrders(allocator: std.mem.Allocator, orders: []const [2]i32) ![]i32 {
        // Assign
        const count = orders.len;
    
        // Allocate
        var queue = try allocator.alloc([2]i32, count);
        var result = try allocator.alloc(i32, count);
    
        // Aftermath
        defer allocator.free(queue);
    
        return result;
    }
    test "all orders done at same time" {
        const allocator = std.testing.allocator;
        const orders = [_][2]i32{ .{ 1, 2 }, .{ 2, 3 }, .{ 3, 3 } };
        const output = [_]i32{ 1, 2, 3 };
        const result = try jimOrders(allocator, orders[0..]);
        defer allocator.free(result);
        try expectEqualSlices(i32, result, output[0..]);
    }
    
  • + 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