RainX_69

Count Ways to Group Overlapping Ranges | MUST DO | OA LEVEL

Mar 18th, 2023
117
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://leetcode.com/problems/count-ways-to-group-overlapping-ranges/
  2.  
  3. You are given a 2D integer array ranges where ranges[i] = [starti, endi] denotes that all integers between starti and endi (both inclusive) are contained in the ith range.
  4.  
  5. You are to split ranges into two (possibly empty) groups such that:
  6.  
  7. Each range belongs to exactly one group.
  8. Any two overlapping ranges must belong to the same group.
  9. Two ranges are said to be overlapping if there exists at least one integer that is present in both ranges.
  10.  
  11. For example, [1, 3] and [2, 5] are overlapping because 2 and 3 occur in both ranges.
  12. Return the total number of ways to split ranges into two groups. Since the answer may be very large, return it modulo 109 + 7.
  13.  
  14.  
  15.  
  16. Example 1:
  17.  
  18. Input: ranges = [[6,10],[5,15]]
  19. Output: 2
  20. Explanation:
  21. The two ranges are overlapping, so they must be in the same group.
  22. Thus, there are two possible ways:
  23. - Put both the ranges together in group 1.
  24. - Put both the ranges together in group 2.
  25. Example 2:
  26.  
  27. Input: ranges = [[1,3],[10,20],[2,5],[4,8]]
  28. Output: 4
  29. Explanation:
  30. Ranges [1,3], and [2,5] are overlapping. So, they must be in the same group.
  31. Again, ranges [2,5] and [4,8] are also overlapping. So, they must also be in the same group.
  32. Thus, there are four possible ways to group them:
  33. - All the ranges in group 1.
  34. - All the ranges in group 2.
  35. - Ranges [1,3], [2,5], and [4,8] in group 1 and [10,20] in group 2.
  36. - Ranges [1,3], [2,5], and [4,8] in group 2 and [10,20] in group 1.
  37.  
  38. ------------------------------------------------------------------------------------------------------------------------------------
  39.  
  40. class Solution {
  41. public:    
  42.     long long fast_Power(int power){
  43.         long long res=1;
  44.         long long x=2;
  45.         while(power>0){
  46.             if(power%2==0){
  47.                 x=(x*x)%1000000007;
  48.                 power/=2;
  49.             }
  50.             else{
  51.                 res=(res*x)%1000000007;
  52.                 power--;
  53.             }
  54.         }  
  55.         return res;
  56.     }
  57.    
  58.     int countWays(vector<vector<int>>& intervals) {
  59.         vector<vector<int>> res;
  60.         sort(intervals.begin(),intervals.end());
  61.         res.push_back(intervals[0]);
  62.         int n=intervals.size();
  63.         for(int i=1;i<n;i++){
  64.             if(res.back()[1]>=intervals[i][0]){ //end>=start of interval, overlapping
  65.                 res.back()[1]=max(res.back()[1],intervals[i][1]);
  66.             }
  67.             else{
  68.                 res.push_back(intervals[i]);
  69.             }
  70.         }
  71.         int nonOverlapping=res.size();
  72.         return fast_Power(nonOverlapping);
  73.     }
  74. };
Advertisement
Add Comment
Please, Sign In to add comment