Hard-题目28:57. Insert Interval

本文介绍了一个关于区间集合的问题,即如何插入一个新的区间并进行合并化简。通过对比Merge Intervals问题,提供了一种解决方案,包括定义区间比较器,并实现区间插入和合并的功能。

题目原文:
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:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].

Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].

This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].
题目大意:
给出一个区间集合,插入一个区间并化简。
题目分析:
跟前面的Hard-题目22:56. Merge Intervals是一回事,但是数据强了一些,要把数组开到40000.
源码:(language:java)

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
public class Solution {
    private class IntervalComparator implements Comparator<Interval> {
        @Override
        public int compare(Interval o1, Interval o2) {
            // TODO Auto-generated method stub
            if(o1.start<o2.start)
                return -1;
            else if(o1.start>o2.start)
                return 1;
            else {
                if(o1.end<o2.end)
                    return -1;
                else if(o1.end>o2.end)
                    return 1;
                else {
                    return 0;
                }
            }
        }

    }
    public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
        intervals.add(newInterval);
        List<Interval> temp = merge(intervals);
        Collections.sort(temp, new IntervalComparator());
        return temp;
    }
    public List<Interval> merge(List<Interval> intervals) {
        boolean[] x = new boolean[40000];
        boolean[] dots = new boolean[40000];
        int max = Integer.MIN_VALUE,min = Integer.MAX_VALUE;
        List<Interval> list = new ArrayList<Interval>();
        for(Interval interval : intervals) {
            for(int i=interval.start;i<interval.end;i++) {
                x[i]=true;
                if(i<min)
                    min=i;
                if(i>max)
                    max=i;
            }
        }

        int i = min;
        while(i<=max) {
            if(!x[i]) {
                i++;
                continue;
            }
            int start=i,end=start;
            while(x[end])
                end++;
            list.add(new Interval(start,end));
            i=end;

        }
        for(Interval interval : intervals) {
            int start = interval.start,end = interval.end;
            if(start==end && !dots[interval.start]) {
                dots[start] = true;
                if(start==0 && !x[0])
                    list.add(new Interval(start, end));
                else if(!x[start] && !x[start-1])
                    list.add(new Interval(start, end));
            }

        }
        return list;
    }
}

成绩:
9ms,24.58%,5ms,26.17%

内容概要:本文介绍了一个基于Matlab的综合能源系统优化调度仿真资源,重点实现了含光热电站、有机朗肯循环(ORC)和电含光热电站、有机有机朗肯循环、P2G的综合能源优化调度(Matlab代码实现)转气(P2G)技术的冷、热、电多能互补系统的优化调度模型。该模型充分考虑多种能源形式的协同转换与利用,通过Matlab代码构建系统架构、设定约束条件并求解优化目标,旨在提升综合能源系统的运行效率与经济性,同时兼顾灵活性供需不确定性下的储能优化配置问题。文中还提到了相关仿真技术支持,如YALMIP工具包的应用,适用于复杂能源系统的建模与求解。; 适合人群:具备一定Matlab编程基础和能源系统背景知识的科研人员、研究生及工程技术人员,尤其适合从事综合能源系统、可再生能源利用、电力系统优化等方向的研究者。; 使用场景及目标:①研究含光热、ORC和P2G的多能系统协调调度机制;②开展考虑不确定性的储能优化配置与经济调度仿真;③学习Matlab在能源系统优化中的建模与求解方法,复现高水平论文(如EI期刊)中的算法案例。; 阅读建议:建议读者结合文档提供的网盘资源,下载完整代码和案例文件,按照目录顺序逐步学习,重点关注模型构建逻辑、约束设置与求解器调用方式,并通过修改参数进行仿真实验,加深对综合能源系统优化调度的理解。
分析一下:Line 30048: 03-24 15:41:07.295 5535 6980 D BluetoothGatt: onConnectionUpdated() - Device=EF:B8:39:25:0B:28 interval=6 latency=0 timeout=500 status=0 Line 30178: 03-24 15:41:08.149 5535 6980 D BluetoothGatt: onConnectionUpdated() - Device=EF:B8:39:25:0B:28 interval=36 latency=0 timeout=500 status=0 Line 30188: 03-24 15:41:08.229 5535 5535 D BluetoothGatt: discoverServices() - device: XX:XX:XX:XX:0B:28 Line 30190: 03-24 15:41:08.235 5535 6980 D BluetoothGatt: onSearchComplete() = Device=EF:B8:39:25:0B:28 Status=0 Line 30203: 03-24 15:41:08.236 5535 5535 D BluetoothGatt: configureMTU() - device: XX:XX:XX:XX:0B:28 mtu: 512 Line 30212: 03-24 15:41:08.238 5535 6980 D BluetoothGatt: onConfigureMTU() - Device=EF:B8:39:25:0B:28 mtu=247 status=0 Line 30220: 03-24 15:41:08.239 5535 5535 D BluetoothGatt: setCharacteristicNotification() - uuid: 00002760-08c2-11e1-9073-0e8ac72e0012 enable: true Line 31018: 03-24 15:41:11.656 5535 6980 D BluetoothGatt: onConnectionUpdated() - Device=EF:B8:39:25:0B:28 interval=12 latency=0 timeout=400 status=0 Line 31215: 03-24 15:41:12.591 5535 5535 D BluetoothGatt: setCharacteristicNotification() - uuid: 00002760-08c2-11e1-9073-0e8ac72e0015 enable: true Line 41166: 03-24 15:41:36.464 5535 5535 D BluetoothGatt: cancelOpen() - device: XX:XX:XX:XX:0B:28 Line 41174: 03-24 15:41:36.469 5535 5535 D BluetoothGatt: close() Line 41175: 03-24 15:41:36.469 5535 5535 D BluetoothGatt: unregisterApp() - mClientIf=6 Line 41211: 03-24 15:41:36.482 5535 14917 D BluetoothGatt: onClientConnectionState() - status=0 clientIf=6 connected=false device=EF:B8:39:25:0B:28 Line 60027: 03-24 15:42:25.021 5535 5535 D BluetoothGatt: connect() - device: XX:XX:XX:XX:0B:28, auto: false, eattSupport: false Line 60028: 03-24 15:42:25.021 5535 5535 D BluetoothGatt: registerApp() Line 60029: 03-24 15:42:25.022 5535 5535 D BluetoothGatt: registerApp() - UUID=65f6d13c-55fd-43aa-a124-f2c161c6d710 Line 60036: 03-24 15:42:25.035 5535 26044 D BluetoothGatt: onClientRegistered() - status=0 clientIf=6 Line 60604: 03-24 15:42:25.746 5535 26044 D BluetoothGatt: onClientConnectionState() - status=0 clientIf=6 connected=true device=EF:B8:39:25:0B:28 Line 61034: 03-24 15:42:26.209 5535 26044 D BluetoothGatt: onConnectionUpdated() - Device=EF:B8:39:25:0B:28 interval=6 latency=0 timeout=500 status=0 Line 61759: 03-24 15:42:27.358 5535 5535 D BluetoothGatt: discoverServices() - device: XX:XX:XX:XX:0B:28 Line 61851: 03-24 15:42:27.491 5535 26044 D BluetoothGatt: onSearchComplete() = Device=EF:B8:39:25:0B:28 Status=0 Line 61863: 03-24 15:42:27.496 5535 5535 D BluetoothGatt: configureMTU() - device: XX:XX:XX:XX:0B:28 mtu: 512 Line 61881: 03-24 15:42:27.527 5535 26044 D BluetoothGatt: onConfigureMTU() - Device=EF:B8:39:25:0B:28 mtu=247 status=0 Line 61891: 03-24 15:42:27.530 5535 5535 D BluetoothGatt: setCharacteristicNotification() - uuid: 00002760-08c2-11e1-9073-0e8ac72e0012 enable: true Line 61905: 03-24 15:42:27.568 5535 26044 D BluetoothGatt: onConnectionUpdated() - Device=EF:B8:39:25:0B:28 interval=36 latency=0 timeout=500 status=0 Line 62298: 03-24 15:42:28.606 5535 5535 D BluetoothGatt: setCharacteristicNotification() - uuid: 00002760-08c2-11e1-9073-0e8ac72e0015 enable: true Line 64132: 03-24 15:42:31.136 5535 5559 D BluetoothGatt: onConnectionUpdated() - Device=EF:B8:39:25:0B:28 interval=12 latency=0 timeout=400 status=0
03-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值