Hackerrank – Popis problému
Celý popis zadania sa nacháza – Hackerrank.
Riešenie
Je potrebné nájsť najvyššiu prekážku v pretekoch. Ak je väčšia ako k
, urobiť ich rozdiel a odstaneme počet nápojov, ktoré stačí vypiť. Inak vypíšeme 0
.
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 13 14 |
object TheHurdleRace extends App { val sc = new java.util.Scanner (System.in) val n = sc.nextInt() val k = sc.nextInt() var height = new Array[Int](n) for(height_i <- 0 until n) { height(height_i) = sc.nextInt() } val maxHeight = height.max val beverages = if(maxHeight > k) maxHeight - k else 0 println(beverages) } |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Scanner; public class HurdleRace { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int n = stdin.nextInt(); int k = stdin.nextInt(); int maxHeight = 0; for(int i = 0; i < n; i++) { int height = stdin.nextInt(); if(height > maxHeight) { maxHeight = height; } } if(maxHeight > k) { System.out.println(maxHeight - k); } else { System.out.println(0); } stdin.close(); } } |
Javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
'use strict'; const processData = input => { const lines = input.split('\n'); const [n, k] = lines[0].split(' ').map(i => parseInt(i)); const heights = lines[1].split(' ').map(i => parseInt(i)); let maxHeight = 0; heights.forEach(h => { maxHeight = (h > maxHeight) ? h : maxHeight; }); const beverages = maxHeight > k ? (maxHeight - k) : 0; console.log(beverages); }; process.stdin.resume(); process.stdin.setEncoding("ascii"); let _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |
Ruby
1 2 3 4 5 6 7 8 |
(n, k) = gets.split.map(&:to_i) heights = gets.split.map(&:to_i) max_height = heights.max beverages = 0 beverages = (max_height - k) if max_height > k puts beverages |