Advertisement
amt

FBDS

amt
Oct 19th, 2024
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. / * Given A and B two interval lists, A has no overlap inside A and B has no overlap inside B. Write the function to merge two interval lists, output the result with no overlap.
  2.  
  3. For example,
  4. A= [1,5], [10,14], [16,18]
  5. B= [2,6], [8,10], [11,20]
  6.  
  7.  
  8. output [1,6], [8, 20]
  9.  
  10. Solve in JavaScript and tell space and time complexity
  11.  
  12. ChatGPT said:
  13. ChatGPT
  14. To solve the problem of merging two interval lists A and B, where both lists have no overlap within themselves, the approach is to:
  15.  
  16. Merge both lists into one combined list.
  17. Sort the combined list by the start of each interval.
  18. Iterate over the combined list and merge overlapping intervals as needed.
  19. Approach:
  20. Merge the two lists: Combine both interval lists into one.
  21. Sort the intervals: Sort the intervals based on the start time (if two intervals have the same start time, the interval with the earlier end time comes first).
  22. Merge the intervals: Iterate over the sorted intervals, merging them if they overlap.
  23.  
  24. Explanation:
  25. Combining the intervals: const intervals = [...A, ...B] merges the two input arrays.
  26. Sorting: intervals.sort((a, b) => a[0] - b[0] || a[1] - b[1]); sorts the intervals by start time, and if two intervals have the same start time, it sorts them by the end time.
  27. Merging: The algorithm iterates through the sorted intervals and checks if the next interval overlaps with the current one. If they overlap, it merges them by updating the end of the current interval to the maximum end of the two. If they do not overlap, it adds the current interval to the result and moves on to the next interval.
  28. Time Complexity:
  29. Sorting the combined list of intervals takes O((m + n) log(m + n)), where m is the number of intervals in list A, and n is the number of intervals in list B.
  30. Merging the intervals takes O(m + n) because each interval is processed once.
  31. Thus, the overall time complexity is O((m + n) log(m + n)).
  32.  
  33. Space Complexity:
  34. We are using extra space for the combined list of intervals and the result, so the space complexity is O(m + n).
  35. Thus, the space complexity is O(m + n).
  36. */
  37. function mergeIntervals(A, B) {
  38.     // Combine both lists
  39.     const intervals = [...A, ...B];
  40.  
  41.     // Sort intervals by start time, if start time is the same, sort by end time
  42.     intervals.sort((a, b) => a[0] - b[0] || a[1] - b[1]);
  43.  
  44.     const result = [];
  45.     let currentInterval = intervals[0];
  46.  
  47.     // Iterate over the intervals and merge as needed
  48.     for (let i = 1; i < intervals.length; i++) {
  49.         const [currentStart, currentEnd] = currentInterval;
  50.         const [nextStart, nextEnd] = intervals[i];
  51.  
  52.         if (nextStart <= currentEnd) {
  53.             // If intervals overlap, merge them
  54.             currentInterval = [currentStart, Math.max(currentEnd, nextEnd)];
  55.         } else {
  56.             // Otherwise, add the current interval to the result and update currentInterval
  57.             result.push(currentInterval);
  58.             currentInterval = intervals[i];
  59.         }
  60.     }
  61.  
  62.     // Add the last merged interval
  63.     result.push(currentInterval);
  64.  
  65.     return result;
  66. }
  67.  
  68. // Example usage:
  69. const A = [[1, 5], [10, 14], [16, 18]];
  70. const B = [[2, 6], [8, 10], [11, 20]];
  71. console.log(mergeIntervals(A, B));  // Output: [ [1, 6], [8, 20] ]
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement