时间区间计算实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
 * Created by shun on 2020/12/3.
 * 时间区间 计算算法 实现
 * 时间段的插入
 * 时间段的删除
 */
public class IntervalAlgorithm {
 
 
    public static List<LocalDate[]> insertDate(List<LocalDate[]> intervals, LocalDate[] newInterval){
        List<long[]> insert = insert(listToArray(intervals), new long[]{newInterval[0].toEpochDay(), newInterval[1].toEpochDay()});
        return toDateList(insert);
    }
 
    public static List<LocalDate[]> removeDate(List<LocalDate[]> intervals, LocalDate[] toBeRemoved){
        List<long[]> removed = removeInterval(listToArray(intervals), new long[]{toBeRemoved[0].toEpochDay(), toBeRemoved[1].toEpochDay()});
        return toDateList(removed);
    }
 
    /**
     * 插入新的时间区间
     * @param intervals 已有区间
     * @param newInterval 新区间
     * @return 插入后的新区间
     */
    public static List<long[]> insert(long[][] intervals, long[] newInterval) {
        long left = newInterval[0];
        long right = newInterval[1];
        boolean placed = false;
        List<long[]> ansList = new ArrayList<long[]>();
        for (long[] interval : intervals) {
            if (interval[0] > right) {
                // 在插入区间的右侧且无交集
                if (!placed) {
                    addInterval(ansList, new long[]{left, right});
                    placed = true;
                }
                addInterval(ansList, interval);
            } else if (interval[1] < left) {
                // 在插入区间的左侧且无交集
                addInterval(ansList, interval);
            } else {
                // 与插入区间有交集,计算它们的并集
                left = Math.min(left, interval[0]);
                right = Math.max(right, interval[1]);
            }
        }
        if (!placed) {
            addInterval(ansList, new long[]{left, right});
        }
//        LocalDate[][] ans = new LocalDate[ansList.size()][2];
//        for (int i = 0; i < ansList.size(); ++i) {
//            ans[i] = ansList.get(i);
//        }
        return ansList;
    }
 
 
    /**
     * 删除区间
     * @param intervals
     * @param toBeRemoved
     * @return
     */
    public static List<long[]> removeInterval(long[][] intervals, long[] toBeRemoved) {
        List<long[]> res = new ArrayList();
        for (long[] inter : intervals) {
            if (inter[1] <= toBeRemoved[0] || inter[0] >= toBeRemoved[1]) {//不再区间就不变
                addInterval(res, new long[]{inter[0], inter[1]});
            } else {
                //在区间
                if (inter[0] < toBeRemoved[0] ) addInterval(res, new long[]{inter[0], toBeRemoved[0]});//右相交
                if (inter[1] > toBeRemoved[1] ) addInterval(res, new long[]{toBeRemoved[1], inter[1]});//左相交
            }
        }
 
        return res;
    }
 
 
    private static long[][] listToArray( List<LocalDate[]> list){
        long[][] ans = new long[list.size()][2];
        for (int i = 0; i < list.size(); ++i) {
            ans[i] = new long[]{list.get(i)[0].toEpochDay(), list.get(i)[1].toEpochDay()};
        }
        return ans;
    }
 
    public static List<LocalDate[]> toDateList(List<long[]> array){
        List<LocalDate[]> list = Lists.newArrayList();
        for (int i = 0; i < array.size(); i++) {
            list.add( new LocalDate[]{LocalDate.ofEpochDay(array.get(i)[0]), LocalDate.ofEpochDay(array.get(i)[1])});
        }
        return list;
    }
 
    //过滤掉当前时间以前的区间
    private static void addInterval(List<long[]> list, long[] newInterval){
        if (newInterval[1] > LocalDate.now().toEpochDay() ){
            if (newInterval[0] < LocalDate.now().toEpochDay()) {
                newInterval[0] = LocalDate.now().toEpochDay();
            }
            list.add(newInterval);
        }
    }
 
 
    public static void main(String[] args) {
        List<LocalDate[]> removeInterval = IntervalAlgorithm.removeDate(
                Lists.newArrayList(
                        new LocalDate[]{LocalDate.of(2019, 01, 01), LocalDate.of(2020, 03, 01)},
                        new LocalDate[]{LocalDate.of(2021, 01, 01), LocalDate.of(2021, 03, 01)},
                        new LocalDate[]{LocalDate.of(2021, 05, 01), LocalDate.of(9999, 01, 01)}
                        ),
                new LocalDate[]{LocalDate.of(2021, 06, 01), LocalDate.of(9999, 01, 01)}
        );
 
        System.out.println(JSON.toJSONString(removeInterval));
 
 
        List<LocalDate[]> insert = IntervalAlgorithm.insertDate(
                removeInterval,
                new LocalDate[]{LocalDate.of(2021, 06, 01), LocalDate.of(9999, 01, 01)});
 
 
        System.out.println(JSON.toJSONString(insert));
    }
 
}

 

posted @   然_默  阅读(458)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示