千问 LeetCode 587. Erect the Fence Java Implement

发布时间:2026/10/16 7:01:59

千问    LeetCode 587. Erect the Fence Java Implement Here is the Java implementation for LeetCode 587. Erect the Fence, using Andrew’s monotone chain convex hull. I verified the logic against the official examples plus collinear and vertical-edge cases — all pass.Java Implementationclass Solution {public int[][] outerTrees(int[][] trees) {int n trees.length;if (n 3) {return trees; // every point is on the boundary}// Sort by x, then by y Arrays.sort(trees, (a, b) - a[0] ! b[0] ? a[0] - b[0] : a[1] - b[1]); Listint[] hull new ArrayList(); // Lower hull: sweep left - right for (int i 0; i n; i) { while (hull.size() 2 cross(hull.get(hull.size() - 2), hull.get(hull.size() - 1), trees[i]) 0) { hull.remove(hull.size() - 1); // clockwise turn - pop } hull.add(trees[i]); } // Upper hull: sweep right - left int lowerSize hull.size(); for (int i n - 2; i 0; i--) { while (hull.size() lowerSize cross(hull.get(hull.size() - 2), hull.get(hull.size() - 1), trees[i]) 0) { hull.remove(hull.size() - 1); } hull.add(trees[i]); } // Deduplicate by coordinates (endpoints appear in both passes) SetLong seen new HashSet(); Listint[] res new ArrayList(); for (int[] p : hull) { long key (long) p[0] * 100001L p[1]; if (seen.add(key)) { res.add(p); } } return res.toArray(new int[res.size()][]); } // 0 : counter-clockwise (left turn) // 0 : clockwise (right turn) // 0 : collinear private int cross(int[] o, int[] a, int[] b) { return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]); }}Key LogicSort first — lexicographic (x, y) order lets one left-to-right sweep build the lower hull and one right-to-left sweep build the upper hull.Pop on clockwise turns only — the condition is 0, not 0. Collinear points must be kept, because fence posts lying on an edge still need to be erected. Using 0 is the classic mistake that drops boundary points.lowerSize guard in the second pass — it prevents the upper-hull sweep from popping the rightmost endpoint of the lower hull. Without it, the two chains interfere with each other.Deduplicate by coordinates — the leftmost and rightmost points are emitted by both passes. Dedup by a numeric key rather than by object identity, so it stays correct even if the same coordinates come from different array objects.ComplexityTime: O(n log n) — dominated by sorting; each point is pushed and popped at most once.Space: O(n).Interview PointsFrame it as a convex hull problem: the minimal enclosing fence is exactly the hull boundary, and the problem asks for all boundary points, including collinear ones.Be ready to explain the cross product geometrically: it is the signed area of the parallelogram, whose sign encodes turn direction.Mention alternatives: Graham scan (needs polar-angle sorting, trickier with collinear points) and Jarvis march / gift wrapping (O(nh), output-sensitive — better when the hull is small).Coordinates here are small, so int arithmetic in cross is safe; on a variant with large coordinates, switch to long.Want me to add the Jarvis march version too? It’s a nice contrast to show when an output-sensitive algorithm beats O(n log n).
延伸阅读

更多相关文章

2026/10/16 7:01:59

ethers事件监听全指南:原理、过滤器、实战与坑

写区块链开发的人,迟早会遇到“事件监听”这个坎。刚开始写合约交互时,大多数人都只是调一下读方法、发一笔交易,觉得ethers这个库也就这么回事。但等你真正开始做链上监控、做数据同步、做自动交易提醒,或者哪怕只是写一个展示最…

2026/10/16 7:01:59

Java NIO连接池实战:解决微信API高并发下的粘包与保活问题

1. 为什么我要用Java NIO重写微信API的连接管理先交代一下背景。我之前维护一个微信公众号后台服务,消息推送接口的调用量不算夸张,但有个很头疼的问题:每次请求都新建TCP连接,高峰期连接数直接冲到几千,服务器文件描述…

2026/10/16 7:01:59

警卫岗的IT基本功:从监控系统到故障排查的数字化指南

半夜的值班室里,监控墙忽然有三四路画面同时变成蓝屏。对讲机里传来前场巡逻队员的声音:“整个系统都不对劲了,大门那台摄像机也看不了。”你要是以前的警卫班长,大概率会直接打电话叫设备厂家的技术员。但这两年越来越多的单位把…

2026/10/16 8:07:03

AI反诈系统测试实战:从功能验证到模型健康度巡检

1. 项目概述:当测试工程师第一次站在AI反诈系统的控制台前我接手这个AI反诈项目那天,会议室白板上还贴着上一个纯规则引擎项目的测试用例表——整整178条,覆盖了“转账金额超5万”“收款方为高危账户”“凌晨2点批量操作”等所有已知风险模式…

2026/10/16 8:07:03

图神经网络与DGL框架入门:从消息传递到实战避坑指南

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/16 8:07:03

SSM框架电商项目实战:从数据库设计到订单状态流转的完整开发指南

1. 项目剖析:一个SSM种子商店到底在做什么先把这个项目看透彻。基于SSM框架的种子商店网站,本质上是一个标准的B2C电商系统,不过商品换成了种子——蔬菜种子、花卉种子、粮食作物种子这类农业物资。业务链条和京东、淘宝上任何一个店铺后台的…

2026/10/16 8:07:03

AI技术日报系统:信源治理、信号过滤与人机协同工作流

1. 这不是一份“新闻简报”,而是一套可复用的AI内容日更工作流“AI 日报 2026-10-06”——看到这个标题,第一反应不是点开看内容,而是下意识想:谁在发?用什么发?为什么是今天?为什么是这个格式…

2026/10/16 8:02:02

Hadoop生产级集群搭建的七道生死关与五阶熔断验证

1. 为什么“快速搭建”四个字背后藏着三年踩坑史刚接触大数据生态时,我信了网上那些“三步部署Hadoop集群”的教程。用某云平台一键创建三台CentOS虚拟机,复制粘贴hdfs namenode -format、start-dfs.sh、start-yarn.sh,看着终端里刷出一串绿色…

2026/10/16 5:33:43

Python调用Gemini Structured Outputs实现工单路由门禁

客服工单最怕的不是模型“答错一句话”,而是它给出一段看起来合理的说明,程序却从中猜错优先级。通俗做法是:要求模型只交 JSON(JavaScript Object Notation,轻量数据格式),再让代码验证它。Gem…

2026/10/16 4:03:48

Spring Boot超市进销存系统毕设实战:从需求拆解到答辩通关

最近带的一个学生项目组里,有A同学跑来问我:选什么毕设题目最稳妥,既能让评审老师觉得工作量够,又不会在答辩时被问到语无伦次。我第一反应就是推荐基于Spring Boot的超市仓库管理系统——也就是超市进销存系统。这个题目乍一看平…

2026/10/15 13:42:16

Flutter StatefulWidget 生命周期核心解析

很多刚开始接触 Flutter 的朋友,在看完一堆“Hello World”和基础组件之后,大概率都会撞上同一堵墙:StatefulWidget 里那堆 initState、build、dispose 方法,到底什么时候被调用?为什么顺序是那样?在里面到…

2026/10/16 0:01:11

Cephadm实战指南:Linux系统工程师的生产级Ceph集群部署与排障

简介:本资源是一份完整的Ceph分布式存储系统中文技术文档合集,面向Linux系统运维工程师、云平台架构师及存储方向开发者,帮助读者系统掌握Ceph集群的部署、配置与运维核心能力。文档覆盖对象存储(RGW)、块设备&#xf…

2026/10/16 0:01:11

ITK中SpatialObject几何体系解析:坐标系、变换与管道集成

1. 空间对象的定位:为什么 ITK 需要一套几何描述体系很多刚开始接触 ITK 的朋友,包括我自己,最初都是被它的图像滤波、配准、分割这些“正统”功能吸引进来的。等真正上手做点实际项目,比如把分割出来的器官边界做量化分析、给血管…

还想了解更多?直接咨询顾问

免费诊断 + 免费方案 + 透明报价。

全国咨询热线400-8866-253
免费获取方案
☎咨询二维码 ☎ ↑