Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool isoverlap(vector<int> a,vector<int> b){
- if(a[1]>=b[0]) return true;
- return false;
- }
- vector<int> mergedRegion(vector<int> a,vector<int> b){
- int x=max(a[0],b[0]);
- int y=min(a[1],b[1]);
- vector<int> ans={x,y};
- return ans;
- }
- int solve(vector<vector<int>>& I) {
- sort(I.begin(),I.end());
- int n=I.size();
- if(n==0) return 0;
- int ans=0;
- int i=0;
- while(i<n){
- if(i+1<n and isoverlap(I[i],I[i+1])){
- vector<int> x=mergedRegion(I[i],I[i+1]);
- i+=2;
- while(i<n and isoverlap(x,I[i])){
- x=mergedRegion(x,I[i]);
- i++;
- }
- ans++;
- }
- else{
- i++;
- ans++;
- }
- }
- return ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment