Sanath12345

Untitled

Aug 25th, 2021
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. bool isoverlap(vector<int> a,vector<int> b){
  2. if(a[1]>=b[0]) return true;
  3. return false;
  4. }
  5. vector<int> mergedRegion(vector<int> a,vector<int> b){
  6. int x=max(a[0],b[0]);
  7. int y=min(a[1],b[1]);
  8. vector<int> ans={x,y};
  9. return ans;
  10. }
  11.  
  12. int solve(vector<vector<int>>& I) {
  13. sort(I.begin(),I.end());
  14. int n=I.size();
  15. if(n==0) return 0;
  16. int ans=0;
  17. int i=0;
  18. while(i<n){
  19. if(i+1<n and isoverlap(I[i],I[i+1])){
  20. vector<int> x=mergedRegion(I[i],I[i+1]);
  21. i+=2;
  22. while(i<n and isoverlap(x,I[i])){
  23. x=mergedRegion(x,I[i]);
  24. i++;
  25. }
  26. ans++;
  27. }
  28. else{
  29. i++;
  30. ans++;
  31. }
  32. }
  33. return ans;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment