Sort by

recency

|

2397 Discussions

|

  • + 0 comments
            /*
             * Write your code here.
             */       
            Arrays.sort(keyboards);
            Arrays.sort(drives);
            int res=0;
            for(int i=keyboards.length-1;i>=0;i--){
                System.out.println(keyboards[i]);
                if((b-keyboards[i])>0){
                    int dr=getDrive(keyboards[i],drives,b);
                    System.out.println(dr+" "+keyboards[i]);
                    if(dr>0 && (keyboards[i]+dr)>res)
                         res=(keyboards[i]+dr);
                }
            }
            if(res>0)
                return res;
            return -1;
        }
        static int getDrive(int keyp,int[] d,int b){
            for(int j=d.length-1;j>=0;j--){
                if(keyp+d[j]<=b)
                {
                    return d[j];
                }
            }
            return -1;
        }
    
  • + 0 comments

    Here is my Python code! We first find all possible combinations of keyboards and drives and then find the largest sum of those. I appended -1 so if there is no combination within budget, it returns -1.

    def getMoneySpent(keyboards, drives, b):
        possible = list(itertools.product(*[keyboards, drives]))
        for a in range(len(possible)):
            possible[a] = sum(possible[a])
        inbudget = [x for x in possible if x <= b]
        inbudget.append(-1)
        return max(inbudget)
    
  • + 1 comment

    What is the best time complexity that can be achieved?

  • + 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

    Very simple python solution here

    def getMoneySpent(keyboards, drives, b):
        sortedKeyBoards = sorted(keyboards, reverse=True)
        drives = sorted(drives, reverse=True)
        sumMax = 0
        
        for i in range(len(sortedKeyBoards)):
            for j in range(len(drives)):
                sumTemp = sortedKeyBoards[i] + drives[j]
                if ( sumTemp >= sumMax and sumTemp <= b):
                    sumMax = sumTemp
                    
        return -1 if sumMax == 0 else sumMax