由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - Skyline
相关主题
please help me to solve this question!Leetcode ==> Max Points on a Line, 我的程序到底哪出问题了
TreeMap, TreeSet原来用起来这么爽override/overload/overwrite in Java
Get windows infoComparator Accessor method for SortedSet
java graphics2d 画图请教Top Ten Errors Java Programmers Make(9)
泛型问题Core Java2 Notes (4)
为啥画不出来?answer Re: how HashMap/Hashtable compare key?
java gc之前有没有办法得到notification?how to copy an Object?
这几个函数可以用Generic之类的东西合并么?question about repaint, update and paint
相关话题的讨论汇总
话题: point话题: rect话题: rects话题: int话题: new
进入Java版参与讨论
1 (共1页)
p*****3
发帖数: 488
1
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
public class EPI_15_1 {
static class Rect {
public int lft;
public int rgt;
public int height;

public Rect(int l, int r, int h) {
lft = l;
rgt = r;
height = h;
}
}

static class Point {
public boolean start;
public int pos;
public int height;

public Point(boolean s, int p, int h) {
start = s;
pos = p;
height = h;
}
}

static class PointComparator implements Comparator {
@Override
public int compare(Point o1, Point o2) {
return o1.height - o2.height;
}
}

static void printSkyLine(Rect[] rects) {
if (null == rects)
return;

Map map = new HashMap();
List points = new ArrayList();
for (Rect rect : rects)
{
Point s = new Point(true, rect.lft, rect.height);
Point e = new Point(false, rect.rgt, rect.height);
points.add(s);
points.add(e);
map.put(e, s);
}

Collections.sort(points, new Comparator() {
@Override
public int compare(Point o1, Point o2) {
return o1.pos - o2.pos;
}
});

SortedSet tree = new TreeSet(new PointComparator());
for (Point pt : points) {
int curHeight = tree.isEmpty() ? 0 : tree.last().height;
if (pt.start)
tree.add(pt);
else
tree.remove(map.get(pt));

if (tree.isEmpty())
System.out.println(pt.pos + " ===> " + curHeight);
else if (tree.last().height != curHeight)
System.out.println(pt.pos + " ===> " + tree.last().height);
}
}

public static void main(String[] strs) {
Rect[] rects = new Rect[8];
rects[0] = new Rect(0, 2, 1);
rects[1] = new Rect(1, 5, 3);
rects[2] = new Rect(3, 7, 4);
rects[3] = new Rect(4, 8, 2);
rects[4] = new Rect(6, 13, 3);
rects[5] = new Rect(9, 11, 5);
rects[6] = new Rect(10, 15, 1);
rects[7] = new Rect(12, 14, 2);

printSkyLine(rects);
}

}
p*g
发帖数: 141
2
干啥的

【在 p*****3 的大作中提到】
: import java.util.ArrayList;
: import java.util.Collections;
: import java.util.Comparator;
: import java.util.HashMap;
: import java.util.List;
: import java.util.Map;
: import java.util.SortedSet;
: import java.util.TreeSet;
: public class EPI_15_1 {
: static class Rect {

P*******r
发帖数: 210
3
赞一个,简洁明了。
c********w
发帖数: 2438
4
mark
w*******s
发帖数: 96
5
mark
c*********e
发帖数: 16335
6
你现在做core java还是j2ee?

【在 p*****3 的大作中提到】
: import java.util.ArrayList;
: import java.util.Collections;
: import java.util.Comparator;
: import java.util.HashMap;
: import java.util.List;
: import java.util.Map;
: import java.util.SortedSet;
: import java.util.TreeSet;
: public class EPI_15_1 {
: static class Rect {

1 (共1页)
进入Java版参与讨论
相关主题
question about repaint, update and paint泛型问题
How to wrap line at specific position?为啥画不出来?
怎么控制paint?java gc之前有没有办法得到notification?
how to access the overrided fields of the parent?这几个函数可以用Generic之类的东西合并么?
please help me to solve this question!Leetcode ==> Max Points on a Line, 我的程序到底哪出问题了
TreeMap, TreeSet原来用起来这么爽override/overload/overwrite in Java
Get windows infoComparator Accessor method for SortedSet
java graphics2d 画图请教Top Ten Errors Java Programmers Make(9)
相关话题的讨论汇总
话题: point话题: rect话题: rects话题: int话题: new