zoukankan      html  css  js  c++  java
  • 57. Insert Interval

    package LeetCode_57
    
    /**
     * 57. Insert Interval
     * https://leetcode.com/problems/insert-interval/description/
     *
     * Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
    You may assume that the intervals were initially sorted according to their start times.
    
    Example 1:
    Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
    Output: [[1,5],[6,9]]
     * */
    class Solution {
        /**
         * solution:
         * 1.insertion sort
         * 2.merge intervals
         * Time complexity:O(n), Space complexity:O(n)
         * */
        fun insert(intervals: Array<IntArray>, newInterval: IntArray): Array<IntArray>? {
            //1. find out the position to insert newInterval
            var index = 0
            val list = ArrayList<IntArray>()
            //if newInterval.start > the first one's start
            for (item in intervals) {
                if (newInterval[0] > item[0]) {
                    index++
                }
            }
            for (item in intervals) {
                list.add(item)
            }
            list.add(index, newInterval)
    
            //2.merge intervals
            val resultList = ArrayList<IntArray>()
            for (item in list) {
                //if current start > prev.end, insert into new one
                if (resultList.isEmpty() || item[0] > resultList.get(resultList.size - 1)[1]) {
                    resultList.add(item)
                } else {
                    //change the end value of the last element
                    resultList.get(resultList.size - 1)[1] = Math.max(resultList.get(resultList.size - 1)[1], item[1])
                }
            }
            val size = resultList.size
            val resultArray = Array(size, { IntArray(2) })
            for (i in 0 until size) {
                resultArray.set(i, resultList.get(i))
            }
            /*for (item in resultArray){
                item.forEach { print("$it,") }
            }*/
            return resultArray
        }
    }
  • 相关阅读:
    1113.层级 Walker
    118.防止高度塌陷 Walker
    1114.实战移入切换 Walker
    116.清除浮动 Walker
    Vue使用——vue设置浏览器显示图标
    MFC如何高效地绘图(转)
    Ugly Windows HDU2487 ACM算法设计
    CImage类的介绍与使用 【图像打开,另存为,转为灰度图像(利用cimage方法实现)】
    Ogre 3D 配置
    WPF文本框只允许输入数字[转]
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13152776.html
Copyright © 2011-2022 走看看