알고리즘/해커랭크

해커랭크 - getMoneySpent

leeyuno 2020. 9. 14. 19:30
/*
 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)