Popis problému
Celý popis zadania sa nacháza – Hackerrank.
Riešenie
Nájdeme si minimum zo zadaného poľa od indexu i
po index j
.
Vytvoril som riešenie v týchto programovacích jazykoch:
Všetky riešenia sú dostupné aj na mojom GitHub profile.
Scala
1 2 3 4 5 6 7 8 9 10 11 12 |
object ServiceLane extends App { val lines = io.Source.stdin.getLines().toList val lanes = lines(1).split(" ").map(_.toInt) val segments = lines.drop(2) val vehicles = segments.map(width(lanes, _)) println(vehicles.mkString("\n")) def width(lanes: Array[Int], inputLane: String): Int = { val arr = inputLane.split(" ").map(_.toInt) lanes.slice(arr(0), arr(1) + 1).min } } |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import java.util.Scanner; public class ServiceLane { private static final int BIKE = 1; private static final int TRUCK = 3; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int laneLength = scanner.nextInt(); int testCases = scanner.nextInt(); int[] laneWidth = new int[laneLength]; for (int i = 0; i < laneLength; i++) { laneWidth[i] = scanner.nextInt(); } for (int j = 0; j < testCases; j++) { int laneEntry = scanner.nextInt(); int laneExit = scanner.nextInt(); System.out.println(largestVehicle(laneWidth, laneEntry, laneExit)); } scanner.close(); } private static int largestVehicle(int[] laneWidth, int laneEntry, int laneExit) { int car = TRUCK; while (car > BIKE && laneExit >= laneEntry) { if(laneWidth[laneEntry] < car) { car = laneWidth[laneEntry]; } laneEntry++; } return car; } } |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
function processData(input) { var lines = input.split('\n'); var lanes = lines[1].split(' ').map(function(item) {return parseInt(item)}); for(var i = 2; i < lines.length; i++) { var arr = lines[i].split(' ').map(function(item) {return parseInt(item)}); console.log(findMin(lanes, arr[0], arr[1])); } } function findMin(arr, i, j) { var min = 10000000; for(var k = i; k <= j; k++) { if(arr[k] < min) { min = arr[k]; } } return min; } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); }); |
Ruby
1 2 3 4 5 6 7 |
(length, tests) = gets.split(" ").map{ |i| i.to_i } lanes = gets.split(" ").map{ |i| i.to_i } tests.times do arr = gets.split(" ").map{ |i| i.to_i } puts lanes.slice(arr[0], arr[1] - arr[0] + 1).min end |