HackerRank Jim and the Orders problem solution

In this HackerRank Jim and the Orders problem solution, Jim’s Burgers has a line of hungry customers. Orders vary in the time it takes to prepare them. Determine the order the customers receive their orders. Start by numbering each of the customers from 1 to n, front of the line to the back. You will then be given an order number and a preparation time for each customer.

The time of delivery is calculated as the sum of the order number and the preparation time. If two orders are delivered at the same time, assume they are delivered in ascending customer number order.

HackerRank Jim and the Orders problem solution

Problem solution in Python.

def solve():
    n = int(input())
    a = [(tuple(map(int, input().split()))) for _ in range(n)]
    a = [(i + 1, a[i][0], a[i][1]) for i in range(n)]
    a = [(x[0], x[1] + x[2]) for x in a]
    a = sorted(a, key=lambda x: x[1])
    a = [x[0] for x in a]
    print(" ".join(list(map(str,a))))
    
    
solve()

{“mode”:”full”,”isActive”:false}

Problem solution in Java.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] o=new int[n];
        boolean[]oo=new boolean[n];
        int[] sol=new int[n];
        for(int i=0;i<n;i++)
            {
            o[i]=(in.nextInt()+in.nextInt());
            oo[i]=false;
        }
        for(int c=0;c<n;c++)
            {
           int best=o[c];
            int r=1;
        for(int i=0;i<n;i++)
            {
            
            if(c!=i&&o[c]>o[i])
                {
                r++;
            }
        }
            if(oo[r-1])
                {
                r++;
            }else{
                oo[r-1]=true;
            }
            sol[r-1]=c+1;
        }
        for(int c=0;c<n;c++)
            {
            System.out.print(sol[c]+" "); 
        }
        
        
    }
}

{“mode”:”full”,”isActive”:false}

Problem solution in C++.

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std;
int n;
int main(){
    int i,j,k;
    scanf("%d",&n);
    vector<pair<int,int> > v;
    for(int i = 0; i < n; ++i){
        scanf("%d%d",&j,&k);
        v.push_back(make_pair(j+k, i+1));
    }
    sort(v.begin(), v.end());
    for(int i = 0; i < v.size(); ++i){
        pair<int,int> p = v[i];
        if(i)printf(" ");
        printf("%d", p.second);
    }
    printf("n");
    return 0;
}

{“mode”:”full”,”isActive”:false}

Problem solution in C.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

typedef struct {
    int t, i;
} pair;
pair a[1000001];

int cmp(const void *a, const void *b) {
    return ((pair *)a)->t - ((pair *)b)->t;
}

int main()
{
    int n, i, p, q;
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        scanf("%d%d", &p, &q);
        a[i].i = i;
        a[i].t = p + q;
    }
    qsort(a, n, sizeof(pair), cmp);
    for (i = 0; i < n; i++)
        printf("%d ", a[i].i + 1);
    return 0;
}

{“mode”:”full”,”isActive”:false}

1 thought on “HackerRank Jim and the Orders problem solution”

  1. //build HashMap order as key, and orderNo + prepare time as value
    Map map = new LinkedHashMap<> ();
    for ( int i=0; i > list =
    new LinkedList >(map.entrySet());

    //sort list of HashMap
    Collections.sort(list, new Comparator >() {
    public int compare(Map.Entry o1,
    Map.Entry o2)
    {
    return (o1.getValue()).compareTo(o2.getValue());
    }
    });

    //build results integer list of order
    List results = new ArrayList();
    for (int i = 0; i< list.size(); i++) {
    results.add(list.get(i).getKey());
    }

    return results;

Comments are closed.