Advertisement
zozohoang

Merge Overlap Interval

Mar 21st, 2025
352
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.63 KB | Software | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4.  
  5.  
  6. void PrintArr(const std::vector<int>& arr, bool isBreakLine = true)
  7. {
  8.     for (const auto& i : arr)
  9.     {
  10.         std::cout << i << " ";
  11.     }
  12.    
  13.     if (isBreakLine)
  14.         std::cout << "\n";
  15. }
  16.  
  17. void Swap(int* num1, int* num2)
  18. {
  19.     int tmp = *num1;
  20.     *num1 = *num2;
  21.     *num2 = tmp;
  22. }
  23.  
  24. /*  Merge Overlapping Interval
  25.     [6,10], [7,9]
  26. */
  27.  
  28. bool CheckOverlapping(const std::vector<int>& arr1, const std::vector<int>& arr2)
  29. {
  30.     bool ret = false;
  31.    
  32.     if (arr1[0] <= arr2[0])
  33.     {
  34.         if (arr2[0] <= arr1[1])
  35.             ret = true;
  36.     }
  37.     else
  38.     {
  39.         if (arr1[0] <= arr2[1])
  40.             ret = true;
  41.     }
  42.    
  43.     return ret;
  44. }
  45.  
  46. std::vector<int> MergeInterval(const std::vector<int>& arr1, const std::vector<int>& arr2)
  47. {
  48.     std::vector<int> mergeInterval(2, 0);
  49.    
  50.     mergeInterval[0] = (arr1[0] <= arr2[0]) ? arr1[0] : arr2[0];
  51.     mergeInterval[1] = (arr1[1] >= arr2[1]) ? arr1[1] : arr2[1];
  52.    
  53.     return mergeInterval;
  54. }
  55.  
  56. std::vector<std::vector<int>> PairInterval(const std::vector<std::vector<int>>& arr1, const std::vector<std::vector<int>>& arr2)
  57. {
  58.     std::vector<std::vector<int>> ret;
  59.    
  60.     if (arr1.size() == 1 && arr2.size() == 1)
  61.     {
  62.         if (arr1[0][0] > arr2[0][0])
  63.         {
  64.             ret.emplace_back(arr2[0]);
  65.             ret.emplace_back(arr1[0]);
  66.         }
  67.         else
  68.         {
  69.             ret.emplace_back(arr1[0]);
  70.             ret.emplace_back(arr2[0]);
  71.         }
  72.        
  73.         return ret;
  74.     }
  75.    
  76.     int i = 0, j = 0;
  77.    
  78.     while (i < arr1.size() && j < arr2.size())
  79.     {
  80.         if (arr1[i][0] < arr2[j][0])
  81.         {
  82.             ret.emplace_back(arr1[i]);
  83.             i++;
  84.         }
  85.         else
  86.         {
  87.             ret.emplace_back(arr2[j]);
  88.             j++;
  89.         }
  90.     }
  91.    
  92.     for (int m = i; m < arr1.size(); m++)
  93.     {
  94.         ret.emplace_back(arr1[m]);
  95.     }
  96.    
  97.     for (int n = j; n < arr2.size(); n++)
  98.     {
  99.         ret.emplace_back(arr2[n]);
  100.     }
  101.    
  102.     return ret;
  103. }
  104.  
  105.  
  106. std::vector<std::vector<int>> SortInterval(std::vector<std::vector<int>>& intervals)
  107. {
  108.     if (intervals.size() == 1)
  109.         return intervals;
  110.        
  111.     int mid = intervals.size() / 2;
  112.    
  113.     std::vector<std::vector<int>> leftIntervals(intervals.begin(), intervals.begin() + mid);
  114.     std::vector<std::vector<int>> rightIntervals(intervals.begin() + mid, intervals.end());
  115.    
  116.     auto leftRet = SortInterval(leftIntervals);
  117.     auto rightRet = SortInterval(rightIntervals);
  118.    
  119.     auto pairRet = PairInterval(leftRet, rightRet);
  120.    
  121.     return pairRet;
  122. }
  123.  
  124. /*
  125. Idea: Tìm phần tử nhỏ nhất sau mỗi lần lặp
  126. */
  127. void SelectionSort(std::vector<int>& unsorted_arr)
  128. {
  129.     for (int i = 0; i < unsorted_arr.size(); i++)
  130.     {
  131.         int curMin = unsorted_arr[i];
  132.         int newIdxMin = -1;
  133.        
  134.         for (int j = i + 1; j < unsorted_arr.size(); j++)
  135.         {
  136.             if (unsorted_arr[j] < curMin)
  137.             {
  138.                 newIdxMin = j;
  139.                 curMin = unsorted_arr[j];
  140.             }
  141.         }
  142.        
  143.         if (newIdxMin != -1)
  144.             Swap(&unsorted_arr[i], &unsorted_arr[newIdxMin]);
  145.     }
  146. }
  147.  
  148.  
  149. int main()
  150. {
  151.     std::vector<std::vector<int>> originInterval = {{1,3},{2,6},{8,10},{15,18}};
  152.     std::vector<std::vector<int>> originInterval1 = {{7, 9}, {6,10}, {4, 5}, {1, 3}, {2, 4}};
  153.    
  154.     auto sortedIntervals = SortInterval(originInterval);
  155.    
  156.    
  157.     for(const auto& interval : sortedIntervals)
  158.     {
  159.         std::cout << "interval:{" << interval[0] << ", " << interval[1] << "} ";
  160.     }
  161.    
  162.     std::cout << std::endl;
  163.    
  164.     int idxNonOverlapInterval = 0;
  165.     for (int i = 1; i < sortedIntervals.size(); i++)
  166.     {
  167.         if (CheckOverlapping(sortedIntervals[idxNonOverlapInterval], sortedIntervals[i]))
  168.         {
  169.             sortedIntervals[idxNonOverlapInterval] = MergeInterval(sortedIntervals[idxNonOverlapInterval], sortedIntervals[i]);
  170.         }
  171.         else
  172.         {
  173.             idxNonOverlapInterval++;
  174.             sortedIntervals[idxNonOverlapInterval] = sortedIntervals[i];
  175.         }
  176.     }
  177.    
  178.     for (int j = 0; j <= idxNonOverlapInterval; j++)
  179.     {
  180.         std::cout << "ret: {" << sortedIntervals[j][0] << ", " << sortedIntervals[j][1] << "} ";
  181.     }
  182.    
  183.     std::cout << std::endl;
  184.    
  185.    
  186.    
  187.     //---------------------------------------------------------------------------------------
  188.     std::vector<int> unsorted_arr = {11, 4, 27, 34, 43, 17, 13, 17, 31, 12};
  189.     SelectionSort(unsorted_arr);
  190.     PrintArr(unsorted_arr);
  191. }
Tags: dsa
Advertisement
Comments
  • responsive02
    4 days
    # text 2.09 KB | 0 0
    1. https://katfile.com/47dnpxn71ghh/defloration_of_rebecca.mp4.html
    2.  
    3. https://katfile.com/smcxklrxq2bu/t33n_hottie_hippie.mp4.html
    4. https://katfile.com/2m534lsws01y/1yo-_renee_roulette.mp4.html
    5.  
    6. https://katfile.com/1cufobt46nkl/teen_fucked_and_spanked.mp4.html
    7. https://katfile.com/9h0ejv5fyw6d/skinny_teen_squirted_on_a_huge_cock.mp4.html
    8.  
    9. https://katfile.com/2c17vu10m7f0/Xvideos_loving_sex_with_teen.mp4.html
    10. https://katfile.com/21h5dbudizpq/little_puusy.mp4.html
    11.  
    12. https://katfile.com/311arifdnur0/stepdaughter_is_fucked.mp4.html
    13. https://katfile.com/io0wfnl7mdk6/eos__arcel.mp4.html
    14.  
    15. https://katfile.com/khn7888qjnn3/exxxtra_t.mp4.html
    16. https://katfile.com/vzfutxpkkmgk/you_doing_i_am_t.mp4.html
    17.  
    18. https://katfile.com/23eazz88040p/girl_masturbation_pussy.mp4.html
    19. https://katfile.com/m7tkkbbgfy9e/exciting_teenie.mp4.html
    20.  
    21. https://katfile.com/9qs94as6iix4/774442[pt.mp4.html
    22. https://katfile.com/bw4nby0d4hfl/analj.mp4.html
    23. https://katfile.com/7us4r0bsconi/cam_5801.mp4.html
    24. https://katfile.com/36s00ol8nv1q/hot_play_webcam_teen_masturbation_in_front.mp4.html
    25. https://katfile.com/110ld42x6d7w/morning_teen_masturb.mp4.html
    26. https://katfile.com/mjk1qxqefaii/orrga01.MP4.html
    27. https://katfile.com/gon211e3ldm9/orrga02.MOV.html
    28. https://katfile.com/41fuahxsgv9j/orrga3.mp4.html
    29. https://katfile.com/si951brc7wai/teen_cocksucker.mp4.html
    30. https://katfile.com/i6ejhnfmzkmy/VID_201_018.mp4.html
    31. https://katfile.com/fi4x6r4tgkqm/darling_tomoyo_isumi_fucks_a_gu.mp4.html
    32. https://katfile.com/ti4wk9bbnwlr/defloration_t.mp4.html
    33. https://katfile.com/io0wfnl7mdk6/eos__arcel.mp4.html
    34. https://katfile.com/m7tkkbbgfy9e/exciting_teenie.mp4.html
    35. https://katfile.com/khn7888qjnn3/exxxtra_t.mp4.html
    36. https://katfile.com/23eazz88040p/girl_masturbation_pussy.mp4.html
    37. https://katfile.com/110ld42x6d7w/morning_teen_masturb.mp4.html
    38. https://katfile.com/dy8t4x1qh63k/my_best_friend_was_a_virgin.mp4.html
    39. https://katfile.com/311arifdnur0/stepdaughter_is_fucked.mp4.html
    40. https://katfile.com/si951brc7wai/teen_cocksucker.mp4.html
    41. https://katfile.com/air1cunyil6q/Xxxx_.t.mp4.html
    42. https://katfile.com/vzfutxpkkmgk/you_doing_i_am_t.mp4.html
Add Comment
Please, Sign In to add comment
Advertisement