[Random Coding]Interval Related Questions

對於每一個end point都生成了新的interval,所以最後一步要把相鄰並且weight相同的interval合併。

本例中,res中依然存在可以合併的interval。

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.PriorityQueue;

public class WeightedInterval {
	public static class Interval {
		int start;
		int end;
		int weight;

		Interval(int start, int end, int weight) {
			this.start = start;
			this.end = end;
			this.weight = weight;
		}
	}

	public ArrayList<Interval> solve(ArrayList<Interval> input) {
		ArrayList<Node> nodes = new ArrayList<Node>();
		for (int i = 0; i < input.size(); i++) {
			nodes.add(new Node(input.get(i).start, true, input.get(i).weight));
			nodes.add(new Node(input.get(i).end, false, input.get(i).weight));
		}
		Comparator<Node> comp = new Comparator<Node>() {
			public int compare(Node node1, Node node2) {
				if (node1.pos > node2.pos)
					return 1;
				else if (node1.pos == node2.pos) {
					if (node1.start)
						return -1;
					else if (node2.start)
						return 1;
					else
						return -1;
				} else
					return -1;
			}
		};
		Collections.sort(nodes, comp);
		Comparator<Integer> heap_comp = new Comparator<Integer>() {
			public int compare(Integer i1, Integer i2) {
				return i2 - i1;
			}
		};
		PriorityQueue<Integer> max_heap = new PriorityQueue<Integer>(5,
				heap_comp);
		ArrayList<Interval> res = new ArrayList<Interval>();
		int i = 0;
		int last_pos = -1;
		while (i < nodes.size()) {
			Node cur = nodes.get(i);
			if (last_pos == -1 && cur.start) {
				max_heap.add(cur.weight);
				last_pos = cur.pos;
				i++;
				continue;
			}
			if (cur.start) {
				if (cur.pos != last_pos) {
					res.add(new Interval(last_pos, cur.pos, max_heap.peek()));
					last_pos = cur.pos;
				}
				max_heap.add(cur.weight);
			} else { // cur.start = false
				if (cur.pos != last_pos) {
					res.add(new Interval(last_pos, cur.pos, max_heap.peek()));
					// need to delete weight before deciding last_pos
					max_heap.remove(cur.weight);
					if (max_heap.isEmpty())
						last_pos = -1;
					else
						last_pos = cur.pos;
				} else
					max_heap.remove(cur.weight);
			}
			i++;
		}
		ArrayList<Interval> final_res = new ArrayList<Interval>();
		if (res.size() != 0) {
			Interval cur = res.get(0);
			for (int j = 1; j < res.size(); j++) {
				Interval next = res.get(j);
				if (cur.end == next.start && cur.weight == next.weight)
					cur.end = next.end;
				else {
					final_res.add(cur);
					cur = next;
				}
			}
			final_res.add(cur);
		}
		return final_res;
	}

	public static class Node {
		int pos;
		boolean start;
		int weight;

		Node(int pos, boolean start, int weight) {
			this.pos = pos;
			this.start = start;
			this.weight = weight;
		}
	}

	public static void main(String[] args) {
		ArrayList<Interval> input = new ArrayList<Interval>();
		input.add(new Interval(0, 1, 14));
		input.add(new Interval(0, 5, 17));
		input.add(new Interval(3, 7, 12));
		WeightedInterval test = new WeightedInterval();
		test.solve(input);
	}
}


發佈了127 篇原創文章 · 獲贊 0 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章