• + 0 comments

    JS solution:

    function getMoneySpent(keyboards, drives, b) {
        /*
         * Write your code here.
         */
        keyboards.sort((a, b) => a - b)
        drives.sort((a, b) => b - a)
        
        let maxPrice = -1    
        let kIndex = 0
        let dIndex = 0
        
        while(kIndex < keyboards.length && dIndex < drives.length) {
            let combinedCost = keyboards[kIndex] + drives[dIndex]
            
            if (combinedCost > b) {
                dIndex++
                continue
            }
            
            kIndex++
            
            if (combinedCost > maxPrice) {
                maxPrice = combinedCost
            } 
        }
        
        return maxPrice
    }