Quick actions

cmd+k|ctrl+k

Navigation

Languages

Service Lane - Hackerank

Snippet info

Language

JavaScript

Visibility

public

Author

onifademola

Created

2021-10-19T22:01:10.723915Z

Updated

2021-10-19T22:01:10.723915Z

// https://www.hackerrank.com/challenges/service-lane/problem
/*
 * Complete the 'serviceLane' function below.
 *
 * The function is expected to return an INTEGER_ARRAY.
 * The function accepts following parameters:
 *  1. INTEGER n
 *  2. 2D_INTEGER_ARRAY cases
 */
 
 const n = 4;
 const n2 = 4;
 const width = [2,3,2,1];
 const width2 = [2, 3, 1, 2, 3, 2, 3, 3];
 const cases = [[1,2], [2,4]];
 const cases2 = [[0,3], [4,6], [6,7], [3,5], [0,7]];

function serviceLane(n, cases, width) {
    if (!width) return [];
    let result = [];
    for (let i=0; i < cases.length; i++) {
        let currentMin = Infinity;
        for (let j=(cases[i][0]); j <= (cases[i][1]); j++) {
            currentMin = width[j] < currentMin ? width[j] : currentMin;
        }
        if (currentMin !== Infinity) {
                result.push(currentMin);
        } 
    }
    return result;
}

console.log(serviceLane(n, cases, width))
console.log(serviceLane(n2, cases2, width2))
INFO