• 计算与所有线段都重合的线段数目


    package com.karl.util;

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.List;

    public class TestOverlap {

        private static List<Duration> getDurations() {
            Duration d1 = new Duration(new Node(4L, true), new Node(5L, false));
            Duration d2 = new Duration(new Node(1L, true), new Node(3L, false));
            Duration d3 = new Duration(new Node(2L, true), new Node(3L, false));
            Duration d4 = new Duration(new Node(2L, true), new Node(4L, false));
            Duration d5 = new Duration(new Node(1L, true), new Node(4L, false));
            List<Duration> list = new ArrayList<Duration>();
            list.add(d1);
            list.add(d2);
            list.add(d3);
            list.add(d4);
            list.add(d5);
            return list;
        }

        private static List<Node> getNodes() {
            List<Duration> durations = getDurations();
            List<Node> nodes = new ArrayList<Node>();
            for (Duration d : durations) {
                nodes.add(d.getStart());
                nodes.add(d.getEnd());
            }
            return nodes;
        }

        private static int getOverlap(Duration d) {
            int max = 1;
            int current = 1;
            List<Node> nodes = getNodes();
            Collections.sort(nodes);
            Iterator<Node> it = nodes.iterator();
            while (it.hasNext()) {
                Node node = it.next();
                System.out.println(node);
                if ((node.getValue() >= d.getStart().getValue())
                        && (node.getValue() <= d.getEnd().getValue())) {
                    if (node.isStart()) {
                        current++;
                    } else {
                        current--;
                    }
                    if (current > max) {
                        max = current;
                    }
                }
            }
            return max;
        }

        public static void main(String[] args) {
            Duration d = new Duration(new Node(0L, true), new Node(9L, false));
            System.out.println(getOverlap(d));
        }

    }

    class Duration {
        private Node start;
        private Node end;

        public Node getStart() {
            return start;
        }

        public void setStart(Node start) {
            this.start = start;
        }

        public Node getEnd() {
            return end;
        }

        public void setEnd(Node end) {
            this.end = end;
        }

        public Duration(Node start, Node end) {
            this.start = start;
            this.end = end;
        }

    }

    class Node implements Comparable<Node> {
        private Long value;
        private boolean start;// this flag indicate the start point or the destination point.

        public Node(Long value, boolean start) {
            this.value = value;
            this.start = start;
        }

        public Long getValue() {
            return value;
        }

        public void setValue(Long value) {
            this.value = value;
        }

        public boolean isStart() {
            return start;
        }

        public void setStart(boolean start) {
            this.start = start;
        }

        @Override
        public String toString() {
            return "[" + value + "," + start + "]";
        }

        @Override
        public int compareTo(Node o) {
            return Long.valueOf(this.value).compareTo(Long.valueOf(o.value));
        }

    }
  • 相关阅读:
    echarts中如何使用timeline组件
    vs发布项目webconfig替换语法
    [置顶] MVC输出缓存(OutputCache参数详解)
    signalr中Group 分组群发消息的简单使用
    echarts异步数据加载(在下拉框选择事件中异步更新数据)
    自定义bootstrap样式-9行样式自定义漂亮大气bootstrap导航栏
    OpenCvSharp 图像旋转
    mybatis获取刚刚插入到数据库的数据的id(转载)
    axios 发 post 请求,后端接收不到参数的解决方案(转载)
    sql 时间获取
  • 原文地址:https://www.cnblogs.com/zhonghan/p/2680012.html
Copyright © 2020-2023  润新知