开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 0

leehl/Java

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (3)
master
Development
dev
master
分支 (3)
master
Development
dev
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (3)
master
Development
dev
Java
/
divideconquer
/
SkylineAlgorithm.java
Java
/
divideconquer
/
SkylineAlgorithm.java
SkylineAlgorithm.java 5.30 KB
一键复制 编辑 原始数据 按行查看 历史
github-actions 提交于 2020年10月24日 18:23 +08:00 . Formatted with Google Java Formatter
package divideconquer;
import java.util.ArrayList;
import java.util.Comparator;
/**
* @author dimgrichr
* <p>Space complexity: O(n) Time complexity: O(nlogn), because it is a divide and conquer
* algorithm
*/
public class SkylineAlgorithm {
private ArrayList<Point> points;
/**
* Main constructor of the application. ArrayList points gets created, which represents the sum of
* all edges.
*/
public SkylineAlgorithm() {
points = new ArrayList<>();
}
/** @return points, the ArrayList that includes all points. */
public ArrayList<Point> getPoints() {
return points;
}
/**
* The main divide and conquer, and also recursive algorithm. It gets an ArrayList full of points
* as an argument. If the size of that ArrayList is 1 or 2, the ArrayList is returned as it is, or
* with one less point (if the initial size is 2 and one of it's points, is dominated by the other
* one). On the other hand, if the ArrayList's size is bigger than 2, the function is called
* again, twice, with arguments the corresponding half of the initial ArrayList each time. Once
* the flashback has ended, the function produceFinalSkyLine gets called, in order to produce the
* final skyline, and return it.
*
* @param list, the initial list of points
* @return leftSkyLine, the combination of first half's and second half's skyline
* @see Point
*/
public ArrayList<Point> produceSubSkyLines(ArrayList<Point> list) {
// part where function exits flashback
int size = list.size();
if (size == 1) {
return list;
} else if (size == 2) {
if (list.get(0).dominates(list.get(1))) {
list.remove(1);
} else {
if (list.get(1).dominates(list.get(0))) {
list.remove(0);
}
}
return list;
}
// recursive part of the function
ArrayList<Point> leftHalf = new ArrayList<>();
ArrayList<Point> rightHalf = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
if (i < list.size() / 2) {
leftHalf.add(list.get(i));
} else {
rightHalf.add(list.get(i));
}
}
ArrayList<Point> leftSubSkyLine = produceSubSkyLines(leftHalf);
ArrayList<Point> rightSubSkyLine = produceSubSkyLines(rightHalf);
// skyline is produced
return produceFinalSkyLine(leftSubSkyLine, rightSubSkyLine);
}
/**
* The first half's skyline gets cleared from some points that are not part of the final skyline
* (Points with same x-value and different y=values. The point with the smallest y-value is kept).
* Then, the minimum y-value of the points of first half's skyline is found. That helps us to
* clear the second half's skyline, because, the points of second half's skyline that have greater
* y-value of the minimum y-value that we found before, are dominated, so they are not part of the
* final skyline. Finally, the "cleaned" first half's and second half's skylines, are combined,
* producing the final skyline, which is returned.
*
* @param left the skyline of the left part of points
* @param right the skyline of the right part of points
* @return left the final skyline
*/
public ArrayList<Point> produceFinalSkyLine(ArrayList<Point> left, ArrayList<Point> right) {
// dominated points of ArrayList left are removed
for (int i = 0; i < left.size() - 1; i++) {
if (left.get(i).x == left.get(i + 1).x && left.get(i).y > left.get(i + 1).y) {
left.remove(i);
i--;
}
}
// minimum y-value is found
int min = left.get(0).y;
for (int i = 1; i < left.size(); i++) {
if (min > left.get(i).y) {
min = left.get(i).y;
if (min == 1) {
i = left.size();
}
}
}
// dominated points of ArrayList right are removed
for (int i = 0; i < right.size(); i++) {
if (right.get(i).y >= min) {
right.remove(i);
i--;
}
}
// final skyline found and returned
left.addAll(right);
return left;
}
public static class Point {
private int x;
private int y;
/**
* The main constructor of Point Class, used to represent the 2 Dimension points.
*
* @param x the point's x-value.
* @param y the point's y-value.
*/
public Point(int x, int y) {
this.x = x;
this.y = y;
}
/** @return x, the x-value */
public int getX() {
return x;
}
/** @return y, the y-value */
public int getY() {
return y;
}
/**
* Based on the skyline theory, it checks if the point that calls the function dominates the
* argument point.
*
* @param p1 the point that is compared
* @return true if the point wich calls the function dominates p1 false otherwise.
*/
public boolean dominates(Point p1) {
// checks if p1 is dominated
return (this.x < p1.x && this.y <= p1.y) || (this.x <= p1.x && this.y < p1.y);
}
}
/**
* It is used to compare the 2 Dimension points, based on their x-values, in order get sorted
* later.
*/
class XComparator implements Comparator<Point> {
@Override
public int compare(Point a, Point b) {
return Integer.compare(a.x, b.x);
}
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

All Algorithms implemented in Java
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/leehl/Java.git
git@gitee.com:leehl/Java.git
leehl
Java
Java
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /