Sort by

recency

|

2418 Discussions

|

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

    This doesn't have a typescript option?

  • + 0 comments

    here is python3 solution to the problem sol1: by taking an empty list def getMoneySpent(keyboards, drives, b): l=[] for i in keyboards: for j in drives: if i+j<=b: l.append(i+j) if not l: return -1 else:
    return max(l) ` sol2: since list might end up taking too much storage def getMoneySpent(keyboards, drives, b): spent=-1 for i in keyboards: for j in drives: total=i+j if total<=b: spent= max(spent,total) return spent