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

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

}
原文地址:https://www.cnblogs.com/zhonghan/p/2680012.html