Swift Coding Test Course, Grouping Line Segments

In this course, we will solve one problem to prepare for coding tests using the Swift language. The topic is to divide line segments into groups. We will understand the problem and explain the solution through the following procedures.

Problem Description

The problem is to determine the minimum number of groups into which the given line segments can be divided when there are overlaps. Line segments are expressed by their starting and ending coordinates, and overlapping segments should be grouped together. Each line segment is given in the form [start, end]. For example, it can be given as [1, 3], [2, 5], [6, 8], etc.

Input Format

Multiple line segments are received in the form of an array. For example:

let segments = [[1, 3], [2, 5], [6, 8], [7, 9]]

Output Format

The minimum number of groups needed to divide the segments will be printed. In the above example, it will be 2. ([1, 3] and [2, 5] overlap and belong to the same group, while [6, 8] and [7, 9] are in different groups.)

Solution

To solve the problem, we first need to sort the segments and then group together the overlapping segments. The strategy to solve this problem is as follows:

  1. Sort the segments in ascending order based on their starting points.
  2. Iterate through the sorted segments and check if they overlap with the last segment of the current group.
  3. If they overlap, include them in the same group; if not, create a new group.

Implementation

Now let’s write code in Swift according to the above strategy:

func countGroups(segments: [[Int]]) -> Int {
    guard !segments.isEmpty else { return 0 }
    
    // 1. Sort segments
    let sortedSegments = segments.sorted { $0[0] < $1[0] }
    
    var groups = 1
    var lastEnd = sortedSegments[0][1]

    // 2. Explore segments
    for i in 1..

Code Explanation

The countGroups function takes an array of line segments as input and returns the minimum number of groups. Each step is as follows:

  1. If the array is empty, return 0 for the number of groups.
  2. Sort the input segments based on their starting points.
  3. Set the initial value for the last endpoint as the endpoint of the first segment. Initialize the number of groups to 1.
  4. Iterate through the remaining segments and check if the starting point of the current segment is less than or equal to the endpoint of the last segment to check for overlaps.
  5. If they overlap, update the last endpoint; otherwise, start a new group and increase the number of groups.

Time Complexity Analysis

The time complexity of this algorithm is O(n log n). Sorting the segments takes O(n log n), and iterating through the segments to calculate the groups takes an additional O(n). Therefore, the final time complexity is O(n log n).

Key Points

  • It is important to accurately verify whether the segments overlap.
  • You need to understand how to effectively handle the segments after sorting.

Through this course, I hope you can improve your algorithm problem-solving skills using Swift. The problem and approach above will be useful for preparing for coding tests. I encourage you to practice with additional problems and have opportunities to learn various algorithms!