码迷,mamicode.com
首页 > 其他好文 > 详细

Add Interval

时间:2017-11-30 12:07:54      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:span   ret   写法   sort   err   etc   sum   void   har   

public interface Intervals {

    /**
     * Adds an interval [from, to] into internal structure.
     */
    void addInterval(int from, int to);


    /**
     * Returns a total length covered by intervals.
     * If several intervals intersect, intersection should be counted only once.
     * Example:
     *
     * addInterval(3, 6)
     * addInterval(8, 9)
     * addInterval(1, 5)
     *
     * getTotalCoveredLength() -> 6
     * i.e. [1,5] and [3,6] intersect and give a total covered interval [1,6]
     * [1,6] and [8,9] don‘t intersect so total covered length is a sum for both intervals, that is 6.
     *
     *                   _________
     *                                               ___
     *     ____________
     *
     * 0  1    2    3    4    5   6   7    8    9    10
     *
     */
    int getTotalCoveredLength();
}

版上常见的那个interval题目, 要实现一个接口,实现addInterval和getTotalCoverage两个函数。 基本算是leetcode的两个interval题目的变种, 第一遍bug free写了一个add O(n)和get O(1)的方法, 然后交流了一下之后做了些小优化。 followup是问有没有让add方法更efficient的写法, 然后我说了下add O(1), get O(nlogn)的方法, 然后又讨论了一些优化的问题

// add Time O(1), get Time O(nlgn)

class MyIntervals  {
    List<Length> l = new LinkedList<Length>();

    public void addInterval(int from, int to) {
        l.add(new Length(from,to));
    }


    public int getTotalCoveredLength(){
        if (l == null || l.size() == 0) {
            return 0;
        }
        Comparator<Length> comp = new Comparator<Length>() {
            @Override
            public int compare(Length o1, Length o2) {
                return o1.x == o2.x ? o1.y - o2.y : o1.x - o2.x;

            }
        };// ";"不能漏
        Collections.sort(l, comp);
        int ans = 0;
        ans += l.get(0).y - l.get(0).x;
        for (int i = 1; i < l.size(); i++) {
            if (l.get(i).y <= l.get(i - 1).y) {
                continue;
            }
            if (l.get(i).x < l.get(i - 1).y) {
                ans += l.get(i).y - l.get(i - 1).y;

            } else {
                ans += l.get(i).y - l.get(i).x;
            }
        }

        return ans;
    }

}

class Length{
    public int x, y;
    public Length(int x, int y) {
        this.x = x;
        this.y = y;
    }

}

  // add Time O(n), get Time O(1)

class MyIntervals  {
    List<int[]> intervals = new LinkedList<int[]>();
    int sum = 0;

    public void addInterval(int from, int to) {
            if (intervals == null) {
                int[] temp = new int[]{from, to};
                sum = to - from;
                intervals.add(temp);
                return;
            }
            List<int[]> result = new LinkedList<int[]>();
            int insertPos = 0;
            int len = 0;
            int[] newInterval = new int[]{from, to};
            for (int[] cur : intervals) {
                if (cur[1] < newInterval[0]) {
                    result.add(cur);
                    len += cur[1] - cur[0];
                    insertPos++;
                } else if (cur[0] > newInterval[1]) {
                    result.add(cur);
                    len += cur[1] - cur[0];
                } else {
                    newInterval[0] = Math.min(newInterval[0], cur[0]);
                    newInterval[1] = Math.max(cur[1], newInterval[1]);
                }
            }
            len += newInterval[1] - newInterval[0];
            result.add(insertPos, newInterval);
            intervals = result;
            sum = len;
            return;
        }

        public int getTotalCoveredLength() {
            return sum;
        }
}

  

 

Add Interval

标签:span   ret   写法   sort   err   etc   sum   void   har   

原文地址:http://www.cnblogs.com/apanda009/p/7927904.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!