Hackerrank – Popis problému
Celý popis zadania sa nacháza – Hackerrank.
Riešenie
Nájdeme maximálnu výšku zo všetkých zadaných znakov. Výsledok je súčin dĺžky reťazca a najvyššieho znaku.
Vytvoril som riešenie v týchto programovacích jazykoch:
Všetky riešenia sú dostupné aj na mojom GitHub profile.
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.*; public class Solution { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); Object[] heights = Arrays.stream(stdin.nextLine().split(" ")) .map(Integer::parseInt).toArray(); String word = stdin.nextLine(); int maxHeight = 0; for(int i = 0; i < word.length(); i++) { int charIndex = word.charAt(i) - 'a'; int height = (int) heights[charIndex]; if(height > maxHeight) { maxHeight = height; } } long square = word.length() * maxHeight; System.out.println(square); 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 |
'use strict'; const processData = input => { const lines = input.split('\n'); const heights = lines[0].split(' ').map(i => parseInt(i)); const word = lines[1]; let maxHeight = 0; for(let i = 0; i < word.length; i++) { const charIndex = word[i].codePointAt() - 97; const height = heights[charIndex]; if(height > maxHeight) { maxHeight = height; } } const square = word.length * maxHeight; console.log(square); }; process.stdin.resume(); process.stdin.setEncoding("ascii"); let _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import scala.io.Source object DesignerPdfViewer extends App { val lines = Source.stdin.getLines().toList val heights = lines.head.split(" ").map(_.toInt) val word = lines(1) val characterIndexes = word.map(_ - 'a') val maxHeight = characterIndexes.map(heights(_)).max val square = word.length * maxHeight println(square) } } |
Ruby
1 2 3 4 5 6 7 |
heights = gets.strip.split.map(&:to_i) word = gets.strip charHeights = word.chars.map{|c| heights[c.ord - 97]} max = charHeights.max square = word.size * max puts square |