NicolaBernini / CodingChallengesSolutions

CPP Code Snippets with comments
4 stars 4 forks source link

Hackerrank - Electronic Shop #32

Open NicolaBernini opened 4 years ago

NicolaBernini commented 4 years ago

Overview

Solution about Electronics Shop

Appunto

NicolaBernini commented 4 years ago

Scala


import java.io.PrintWriter

object Solution {

    /*
     * Complete the getMoneySpent function below.
     */
    def getMoneySpent(keyboards: Array[Int], drives: Array[Int], b: Int): Int = {
        /*
         * Write your code here.
         */
         val t2 = for( k <- keyboards; d <- drives ) yield {
           k+d match {
             case x if x <= b => x 
             case _ => 0
           }
         }
         t2.max match {
           case x if x > 0 => x 
           case _ => -1
         }
    }

    def main(args: Array[String]) {
        val stdin = scala.io.StdIn

        val printWriter = new PrintWriter(sys.env("OUTPUT_PATH"))

        val bnm = stdin.readLine.split(" ")

        val b = bnm(0).trim.toInt

        val n = bnm(1).trim.toInt

        val m = bnm(2).trim.toInt

        val keyboards = stdin.readLine.split(" ").map(_.trim.toInt)

        val drives = stdin.readLine.split(" ").map(_.trim.toInt)
        /*
         * The maximum amount of money she can spend on a keyboard and USB drive, or -1 if she can't purchase both items
         */

        val moneySpent = getMoneySpent(keyboards, drives, b)

        printWriter.println(moneySpent)

        printWriter.close()
    }
}

Appunto