LeetCode 352. Data Stream as Disjoint Intervals

Data Stream as Disjoint Intervals

Hard

Given a data stream input of non-negative integers a1, a2, …, an, …, summarize the numbers seen so far as a list of disjoint intervals.

For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, …, then the summary will be:

[1, 1]
[1, 1], [3, 3]
[1, 1], [3, 3], [7, 7]
[1, 3], [7, 7]
[1, 3], [6, 7]

思路

维护一个有序数组存储所有interval,interval的左边界和右边界存在同一个数组中。插入新的元素时二分查找元素应该插入的位置,注意讨论各种边界情况。

代码

/*
 * @lc app=leetcode id=352 lang=java
 *
 * [352] Data Stream as Disjoint Intervals
 */
class SummaryRanges {
    List<Integer> ranges = new ArrayList<Integer>();

    public int[][] toArrays() {
        int i = 0, n = ranges.size()/2;
        int[][] ret = new int[n][2];
        for (i=0; i<n; ++i) {
            ret[i][0] = ranges.get(2*i);
            ret[i][1] = ranges.get(2*i+1);
        }
        return ret;
    }

    /**
    * find the first element position >= target
     */
    private int binarySearch(int target) {
        int n = ranges.size(), l = 0, r = n-1, m = 0;
        if (target > ranges.get(n-1)) {
            return n;
        }
        if (target <= ranges.get(0)) {
            return 0;
        }
        while (l < r) {
            m = (l + r) / 2;
            if (ranges.get(m) < target) {
                l = m + 1;
            } else {
                r = m;
            }
        }
        return l;
    }

    /** Initialize your data structure here. */
    public SummaryRanges() {
        
    }
    
    public void addNum(int val) {
        int n = ranges.size();
        if (n == 0) {
            ranges.add(val);
            ranges.add(val);
            return;
        }
        int idx = binarySearch(val);
        if ((idx & 1) == 0) {   // between intervals
            if (idx < n && val == ranges.get(idx)) {
                return;
            }
            if (idx == n) {
                if (val == ranges.get(n-1) + 1) {
                    ranges.set(n-1, val);
                } else {
                    ranges.add(val);
                    ranges.add(val);
                }
            } else if (idx == 0) {
                if (val == ranges.get(idx) - 1) {
                    ranges.set(0, val);
                } else {
                    ranges.add(0, val);
                    ranges.add(0, val);
                }
            } else {
                if (val == ranges.get(idx) - 1 && val == ranges.get(idx-1) + 1) {
                    ranges.remove(idx-1);
                    ranges.remove(idx-1);
                } else if (val == ranges.get(idx) - 1) {
                    ranges.set(idx, val);
                } else if (val == ranges.get(idx-1) + 1) {
                    ranges.set(idx-1, val);
                } else {
                    ranges.add(idx, val);
                    ranges.add(idx, val);
                }
            }
        }
        return;
    }
    
    public int[][] getIntervals() {
        return toArrays();
    }
}

/**
 * Your SummaryRanges object will be instantiated and called as such:
 * SummaryRanges obj = new SummaryRanges();
 * obj.addNum(val);
 * int[][] param_2 = obj.getIntervals();
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值