본문 바로가기

알고리즘/해커랭크

해커랭크 - getMoneySpent

/*
 int keyboards[n]: the keyboard prices
 int drives[m]: the drive prices
 int b: the budget
 returns -> int: the maximum that can be spent, or  if it is not possible to buy both items
 */
func getMoneySpent(keyboards: [Int], drives: [Int], b: Int) -> Int {
    var results: [Int] = [Int]()

    for keyboard in keyboards {
        for drive in drives {
            if keyboard + drive <= b {
                results.append(keyboard + drive)
            }
        }
    }

    results = results.sorted {
        $0 > $1
    }
    
    if results.count == 0 {
        return -1
    } else {
        return results[0]
    }
}

getMoneySpent(keyboards: [3, 1], drives: [5, 2, 8], b: 10)

'알고리즘 > 해커랭크' 카테고리의 다른 글

해커랭크 - formingMagicSquare  (0) 2020.09.15
해커랭크 - climbingLeaderboard  (0) 2020.09.15
해커랭크 - CountingValleys  (0) 2020.09.14
해커랭크 - SockMerchant  (0) 2020.09.14
해커랭크 - Bon Appetit  (0) 2020.09.14