Sort by

recency

|

2420 Discussions

|

  • + 0 comments
    from itertools import product
    def get_money_spent(keyboards, drives, b):
        possible_sums = [ x + y for x,y in list(product(keyboards, drives)) if x + y <= b ]
        return max(possible_sums) if len(possible_sums) > 0 else -1
    
  • + 0 comments
    def getMoneySpent(b, keyboards, drives):
        # 
        # Write your code here.
        final = []
        for i in keyboards:
            for j in drives:
                if (i+j) <= b:
                    final.append(i+j)
                    
        if len(final) == 0:
            return -1
        else:
            return max(final)
    
  • + 0 comments

    Two-pointer solution:

        keyboards.sort()
        drives.sort(reverse=True)
        
        best = -1
        i = j = 0
        
        while i < len(keyboards) and j < len(drives):
            total = keyboards[i] + drives[j]
            if total > b:
                j += 1
            else:
                if total > best:
                    best = total
                i += 1
        return best
    
  • + 0 comments

    C# static int getMoneySpent(int[] keyboards, int[] drives, int b) { int max = -1; Array.Sort(keyboards); Array.Sort(drives);

     int i = 0;
     int j = drives.Length - 1;
    
     while (i <= keyboards.Length - 1 && j >= 0)
     {
         int sum = keyboards[i] + drives[j];
    
         if (sum > b)
         {
             j--;
         }
         else
         {
             i++;
    
             if (sum > max)
             {
                 max = sum;
             }
         }
     }
    
     return max;
    

    }

  • + 0 comments
    costs = []
    
    for i in keyboards:
        for j in drives:
            costs.append (i+j) if (i+j <= b) else costs.append (0)
    return max(costs) if (max(costs) > 0) else -1