Hackerrank – Popis problému
Celý popis zadania sa nacháza – Hackerrank.
Riešenie
Pozrieť implementáciu. Postup je daný v zadaní
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 15 16 17 18 19 20 21 22 23 |
import scala.io.Source object Solution extends App { val lines = Source.stdin.getLines().toList val points = lines.tail.head.split(" ").map(_.toInt) var mostPoints = points.head var leastPoints = points.head var mostPointsBreak = 0 var leastPointsBreak = 0 points.foreach(point => { if(point > mostPoints) { mostPoints = point mostPointsBreak += 1 } if(point < leastPoints) { leastPoints = point leastPointsBreak += 1 } }) println(mostPointsBreak + " " + leastPointsBreak) } |
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 |
import java.util.*; public class BreakingTheRecords { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int tests = stdin.nextInt(); long[] records = new long[tests]; for(int i = 0; i < tests; i++) { records[i] = stdin.nextLong(); } long mostPoints = records[0]; long leastPoints = records[0]; int mostPointsBreak = 0; int leastPointsBreak = 0; for(int i = 1; i < tests; i++) { long points = records[i]; if(points > mostPoints) { mostPoints = points; mostPointsBreak += 1; } if(points < leastPoints) { leastPoints = points; leastPointsBreak += 1; } } System.out.println(mostPointsBreak + " " + leastPointsBreak); stdin.close(); } } |
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 30 31 |
'use strict'; const processData = input => { const lines = input.split('\n'); const records = lines[1].split(' ').map(i => parseInt(i)); let mostPoints = records[0]; let leastPoints = records[0]; let mostPointsBreak = 0; let leastPointsBreak = 0; for(let i = 1; i < records.length; i++) { let points = records[i]; if(points > mostPoints) { mostPoints = points; mostPointsBreak += 1; } if(points < leastPoints) { leastPoints = points; leastPointsBreak += 1; } } console.log(mostPointsBreak + ' ' + leastPointsBreak); }; process.stdin.resume(); process.stdin.setEncoding("ascii"); let _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |