DEV Community

Minh Hieu
Minh Hieu

Posted on

Service Lane Explain - Solution | Javascript

function serviceLane(n, cases) {
  let result = [];
  const caseSize = cases.length;
  for( let i = 0; i < caseSize; i ++) {
    const start = cases[i][0];
    const end = cases[i][1];
    const min = Math.min(...n.slice(start, end + 1));
    result.push(min);
  }
  return result;

}

serviceLane([2, 3, 1, 2, 3, 2, 3, 3], [ [ 0, 3 ], [ 4, 6 ], [ 6, 7 ], [ 3, 5 ], [ 0, 7 ] ])

// You must change main function for exactly input
function main() {
    const ws = fs.createWriteStream(process.env.OUTPUT_PATH)

    const [n, t] = [...readLine().split(' ')].map(x => parseInt(x, 10))
    const width = readLine().split(' ').map(x => parseInt(x, 10))
    const cases = Array(t).fill(0).map(x => readLine().split(' ').map(y => parseInt(y, 10)))
    let result = serviceLane(width, cases)

    ws.write(result.join("\n") + "\n")
    ws.end()
}

Problem

Top comments (0)