RainX_69

Problem on Queries | Solved using Line Sweep | MUST DO PROBLEM

Feb 28th, 2023 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | Source Code | 0 0
  1. https://practice.geeksforgeeks.org/contest/gfg-weekly-coding-contest-91/problems/#
  2.  
  3. John, owns an inherited jewellery. Since his family is in need of money, he decides to sell that jewellery, but he is unaware at what price he should sell it. So he consulted his N friends and each friend guessed a range [low, high] which can be the price of that diamond according to that friend. John is confused but these guessed ranges. But decides that the price that is suggested by atleast k friends might be the best price to sell at.
  4. John goes to the market and quotes his sell price range [l,r]. John quotes Q queries where each query is a price range quoted by John. Return the number of "best prices" that exist in this range quoted by John.
  5.  
  6. BEST PRICE is a price that is suggested by atleast K friends.
  7.  
  8. Example 1:
  9.  
  10. Input :
  11. n=2
  12. price={{1,3},{2,4}}
  13. k=2
  14. q=1
  15. queries={{1,4}}
  16. Output:
  17. {2}
  18. Explanation:
  19. As price 2 and 3 is suggested by both
  20. the friends. 2 and 3 are suggested by 2 friends and since 2>=k, both of these prices are best price.
  21. So 2 and 3 is best price.
  22.  
  23. Example 2:
  24.  
  25. Input :
  26. n=3
  27. price={{1,3},{3,5},{2,6}}
  28. k=3
  29. q=2
  30. queries={{1,3},{5,6}}
  31. Output:
  32. {1,0}
  33. Explanation:
  34. For query 1 [1,3], price 3 is the only suggested price by atleast k friends. So answer is 1 best price
  35. For query 2 [5,6], there is no best price.
  36.  
  37. ------------------------------------------------------------------------------------------------------------------------------------
  38.  
  39. class Solution {
  40.   public:
  41.     int mx=0;
  42.    
  43.     vector<int> LINE_SWEEP(vector<vector<int>> &price, vector<vector<int>> &queries){
  44.         vector<int> freq(mx+10,0);
  45.         for(auto p: price){
  46.             freq[p[0]]++;
  47.             freq[p[1]+1]--;
  48.         }
  49.         for(int i=1;i<mx+10;i++){
  50.             freq[i]=freq[i]+freq[i-1];
  51.         }
  52.         return freq;
  53.     }
  54.    
  55.     vector<int> bestPrice(int n, vector<vector<int>> price, int k, int q, vector<vector<int>> queries) {
  56.         // line sweep algorithm
  57.         for(auto p: price){
  58.             mx=max(mx,p[1]);
  59.         }
  60.         for(auto q: queries){
  61.             mx=max(mx,q[1]);
  62.         }
  63.        
  64.         vector<int> freq=LINE_SWEEP(price,queries);
  65.        
  66.         int best[mx+10]={0};
  67.         for(int i=1;i<mx+10;i++){  // calculate the number of elements having >=k
  68.             if(freq[i]>=k){
  69.                 best[i]=1;
  70.             }
  71.             best[i]+=best[i-1];
  72.         }
  73.        
  74.         vector<int> res;
  75.         for(auto q: queries){
  76.             int answer=best[q[1]]-best[q[0]-1];
  77.             res.push_back(answer);
  78.         }
  79.         return res;
  80.     }
  81. };
Advertisement
Add Comment
Please, Sign In to add comment