zoukankan      html  css  js  c++  java
  • Daily Coding Problem: Problem #723

    /**
     * This problem was asked by Google.
    Given a set of closed intervals, find the smallest set of numbers that covers all the intervals.
    If there are multiple smallest sets, return any of them.
    For example, given the intervals [0, 3], [2, 6], [3, 4], [6, 9],
    one set of numbers that covers all these intervals is {3, 6}.
     * */
    class Problem_723 {
        /*
        * solution: find out the maximum in left of intervals and minimum in right of intervals,
        * Time complexity:O(n), Space complexity:O(1)
        * */
        fun smallestSet(intervals: Array<IntArray>): IntArray {
            if (intervals == null || intervals.isEmpty()) {
                return intArrayOf()
            }
            var smallMax = Int.MAX_VALUE
            var largeMin = Int.MIN_VALUE
            for (interval in intervals) {
                largeMin = Math.max(largeMin, interval[0])
                smallMax = Math.min(smallMax, interval[1])
            }
            return intArrayOf(smallMax, largeMin)
        }
    }
  • 相关阅读:
    BZOJ 3626: [LNOI2014]LCA(树链剖分+离线处理)
    python备用
    STL的使用。。备忘
    DP专题
    任务
    hdu 网络流题集
    hdu KM匹配题集
    hdu 差分约束题集
    hdu 2sat题集
    Codeforces Round #261 (Div. 2)
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/14034621.html
Copyright © 2011-2022 走看看