본문 바로가기
개발공간

C# Electronics Shop [HackerRank]

by -0o0- 2021. 9. 11.

C# Electronics Shop [HackerRank]

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

class Solution {

    static int getMoneySpent(int[] keyboards, int[] drives, int b) {
        
        //최고 효율을 내는 비용 변수 -1로 선언
        int maxCost=-1;
        
        for(int i=0; i<keyboards.Length; i++)
        {
            for(int j=0; j<drives.Length; j++)
            {
                //보든 경우의 수를 반복하여 buget보다 작으면서 변수 maxCost보다 클 때 maxCost 초기화
                if(keyboards[i]+drives[j]<=b && keyboards[i]+drives[j] > maxCost) 
                {
                    maxCost=keyboards[i]+drives[j];
                }
            }
        }        
        return maxCost;
    }

    static void Main(string[] args) {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

        string[] bnm = Console.ReadLine().Split(' ');

        int b = Convert.ToInt32(bnm[0]);

        int n = Convert.ToInt32(bnm[1]);

        int m = Convert.ToInt32(bnm[2]);

        int[] keyboards = Array.ConvertAll(Console.ReadLine().Split(' '), keyboardsTemp => Convert.ToInt32(keyboardsTemp))
        ;

        int[] drives = Array.ConvertAll(Console.ReadLine().Split(' '), drivesTemp => Convert.ToInt32(drivesTemp))
        ;
        /*
         * The maximum amount of money she can spend on a keyboard and USB drive, or -1 if she can't purchase both items
         */

        int moneySpent = getMoneySpent(keyboards, drives, b);

        textWriter.WriteLine(moneySpent);

        textWriter.Flush();
        textWriter.Close();
    }
}

'개발공간' 카테고리의 다른 글

C# Cats and a Mouse [HackerRank]  (0) 2021.09.12
C# Counting Valleys [HackerRank]  (0) 2021.09.11
C# Drawing Book [HackerRank]  (0) 2021.09.11
C# Sales by Match [HackerRank]  (0) 2021.09.10
C# Bill Division [HackerRank]  (0) 2021.09.10

댓글