Insert Interval, is a LeetCode problem from sorting subdomain. In this post we will see how we can solve this challenge in Swift

Problem Description

You can find the full details of the problem Insert Interval at LeetCode

Solution

If so, save the index, otherwise save intervals

          Second, keep updating a new interval if nuewInterval's end is larger then one interval's start

                 If cannot find more, append the new interval to the result array

          Final Step, append the rest intervals to the result array

Time Complexity: O(n), Space Complexity: O(1),

Definition for an interval.

public class Interval {

public var start: Int

public var end: Int

public init(_ start: Int, _ end: Int) {

    self.start = start

    self.end = end

}

}

Please check the main.swift snippet for the solution.

This solution originally posted at: Github by @soapyigu