jayati

Successful Pairs of Spells and Potions

Apr 1st, 2023
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {
  4.         vector<int> v(spells.size(),0);
  5.         sort(potions.begin(),potions.end());
  6.        
  7.         for(int i=0;i<spells.size();i++)
  8.         {
  9.             int h=potions.size()-1;
  10.             int l=0;
  11.             int mid;
  12.             while(l<=h)
  13.             {
  14.                 mid = l + (h-l)/2;
  15.                
  16.                 if((long long int)spells[i]*(long long int)potions[mid] >= success)
  17.                     h = mid-1;
  18.                
  19.                 else
  20.                     l = mid+1;
  21.             }
  22.            
  23.             v[i] = potions.size()-1-h;
  24.         }
  25.        
  26.         return v;
  27.        
  28.     }
  29. };
Advertisement
Add Comment
Please, Sign In to add comment