bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 757. Set Intersection Size At Least Two

757. Set Intersection Size At Least Two

Leetcode Solutions

Greedy Approach with Sorted Intervals

  1. Sort the intervals by their end points in ascending order. If two intervals have the same end point, sort them by their start points in descending order.
  2. Initialize two pointers, p1 and p2, to -1, representing the last two elements of the current minimum containing set.
  3. Iterate through the sorted intervals.
  4. For each interval, check if the start point is greater than p2. If so, add two new elements to the containing set and update p1 and p2 to the last two elements of the interval.
  5. If the start point is only greater than p1, add one new element to the containing set and update p1 to p2 and p2 to the end point of the interval.
  6. If the start point is less than or equal to p1, continue to the next interval as the current set already satisfies the interval.
  7. After processing all intervals, return the size of the containing set, which is the sum of the increments made during the iteration.
UML Thumbnail

Dynamic Programming Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...