zoukankan      html  css  js  c++  java
  • 计算与所有线段都重合的线段数目

    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));
        }

    }
  • 相关阅读:
    解析大型.NET ERP系统 权限模块设计与实现
    Enterprise Solution 开源项目资源汇总 Visual Studio Online 源代码托管 企业管理软件开发框架
    解析大型.NET ERP系统 单据编码功能实现
    解析大型.NET ERP系统 单据标准(新增,修改,删除,复制,打印)功能程序设计
    Windows 10 部署Enterprise Solution 5.5
    解析大型.NET ERP系统 设计异常处理模块
    解析大型.NET ERP系统 业务逻辑设计与实现
    解析大型.NET ERP系统 多国语言实现
    Enterprise Solution 管理软件开发框架流程实战
    解析大型.NET ERP系统 数据审计功能
  • 原文地址:https://www.cnblogs.com/zhonghan/p/2680012.html
Copyright © 2011-2022 走看看