Sort by

recency

|

2391 Discussions

|

  • + 0 comments

    C#

    static int getMoneySpent(int[] keyboards, int[] drives, int b) {

         int temp =-1;
         foreach(var keyboard in keyboards){
            foreach(var drive in drives){
                if((keyboard + drive) > temp && (keyboard + drive) <=  b)
                temp = keyboard + drive;
            }
    
    
         }
         return temp;
    }
    
  • + 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
    }
    
  • + 0 comments

    Here is my python 3 solution

    maxvalue=-1;
    for keyboar in keyboards :
        for drive in drives :
            total = keyboar+drive
            if total< b and total > maxvalue  :
                maxvalue = total
    
    return maxvalue
    
  • + 0 comments

    Here is my c++ solution, you can find the video here : https://youtu.be/yC-TXToDbD0

    int getMoneySpent(vector<int> keyboards, vector<int> drives, int b) {
        int ans = -1;
        for(int k : keyboards){
            for(int d : drives){
                if(k + d > ans && k + d <= b) ans = k + d;
            }
        }
        return ans;
    }
    
  • + 0 comments

    most simple solution i found

    let pricies = []
        for (const k of keyboards) {
            for (const d of drives) pricies.push(d + k)
        }
        const maxPrice = pricies.filter((p) => p <= b)
        return maxPrice.length > 0 ? Math.max(...maxPrice) : -1