千问    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).

相关新闻

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

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

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

2026/10/16 7:01:59 阅读更多 →
Java NIO连接池实战:解决微信API高并发下的粘包与保活问题

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

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

2026/10/16 7:01:59 阅读更多 →
警卫岗的IT基本功:从监控系统到故障排查的数字化指南

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

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

2026/10/16 7:01:59 阅读更多 →

最新新闻

Agent工程化落地的5个关键问题与实战方案

Agent工程化落地的5个关键问题与实战方案

1. 这不是考算法,而是考你能不能把Agent真正跑起来“请手写一个ReAct框架”——这种问题在三年前可能还出现在某些技术面试里,但今天再问,基本等于在面试一个还没摸过生产环境的在校生。我参与过二十多场面向中高级工程师的Agent方向技术面试…

2026/10/16 7:53:14 阅读更多 →
AI日报系统设计:从信源治理到时间戳工程的全链路实践

AI日报系统设计:从信源治理到时间戳工程的全链路实践

1. 项目概述:这不是一份“新闻简报”,而是一套可复用的AI内容日更系统“AI 日报 2026-10-07”这个标题乍看像某平台每日推送的资讯合集,但真正有经验的人一眼就能看出——它背后必然藏着一套稳定、低干预、可持续输出的自动化内容生产流程。…

2026/10/16 7:53:14 阅读更多 →
LTC681X官方驱动源码全解析:BMS电池采样与均衡控制实践

LTC681X官方驱动源码全解析:BMS电池采样与均衡控制实践

简介:LTC681X系列是ADI面向高精度电池管理系统(BMS)设计的多通道AFE芯片,这套官方驱动源码主要面向电池管理开发者与嵌入式软件工程师,解决多节串联电池电压采样、隔离SPI通信及数据解析等核心难题。资源包采用rar压缩…

2026/10/16 7:53:14 阅读更多 →
AI工程化治理:从Demo到生产级系统的三大核心能力

AI工程化治理:从Demo到生产级系统的三大核心能力

1. 项目概述:这不是写PPT,是给AI应用装上“刹车片”和“导航仪”“治理学习笔记:从Demo到生产——AI 应用的工程化治理”,这个标题里藏着三个被太多人忽略的硬核事实:第一,“Demo能跑通”和“系统能扛住”之…

2026/10/16 7:53:14 阅读更多 →
汇川机器人电脑版示教器:PC端示教与调试实战指南

汇川机器人电脑版示教器:PC端示教与调试实战指南

简介:汇川机器人电脑版示教器软件是面向自动化生产线、汽车制造、3C电子等领域的机器人编程与调试工具,将传统示教器功能迁移至PC端,适合设备工程师、调试人员及工业机器人学习者使用,解决远程控制、离线仿真、故障诊断等实际需求…

2026/10/16 7:53:14 阅读更多 →
GitHub Actions上跑AI Agent:事件驱动自动化实战与避坑指南

GitHub Actions上跑AI Agent:事件驱动自动化实战与避坑指南

最近在做持续集成自动化相关的事情时,我发现一个特别有意思的玩法——把 AI Agent 直接跑在 GitHub Actions 上。说白了,就是用一个开源框架,把传统常驻服务器上的 Agent 拆成按次计费的临时任务,每次由事件触发拉起一个运行环境&…

2026/10/16 7:52:14 阅读更多 →

日新闻

个人 AI Agent 平台接入 TaoToken:统一 Key 与 API 通道的配置大纲

个人 AI Agent 平台接入 TaoToken:统一 Key 与 API 通道的配置大纲

/* 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 0:00:05 阅读更多 →
信号与系统频域分析作业解题指南

信号与系统频域分析作业解题指南

1. 作业背景与核心要求解析信号与系统作为电子信息类专业的核心基础课,其作业设计往往直接关联课程的核心知识框架。2026年春季第六次作业的布置,通常对应着课程进展到频域分析的关键阶段。根据常规教学进度推测,本次作业可能涉及以下核心内容…

2026/10/16 0:00:05 阅读更多 →
Django MVT实战:从零构建图书借阅系统,彻底搞懂Model-View-Template分层

Django MVT实战:从零构建图书借阅系统,彻底搞懂Model-View-Template分层

1. 从一个"四不像"的练手项目说起如果你已经看完了Django的官方教程前几章,跟着敲完了投票应用,大概会有一种"我好像会了,又好像什么都不会"的割裂感。教程里每个知识点都是拆开的——这一章讲模型,下一章讲视…

2026/10/16 0:00:05 阅读更多 →

周新闻

流感时间序列预测实战:ARIMA/LSTM全流程拆解与避坑指南

流感时间序列预测实战:ARIMA/LSTM全流程拆解与避坑指南

简介:基于 ARIMA、LSTM、Transformer 等模型的流感时间序列预测 Python 源码,面向计算机相关专业课程设计与期末大作业学生,以及项目实战学习者。内容覆盖预处理、平稳性检验、定阶、残差分析、多模型对比预测的完整时序建模流程,…

2026/10/15 16:18:54 阅读更多 →
影刀RPA新手教程:键盘模拟输入实战——输入文本与模拟按键的区别

影刀RPA新手教程:键盘模拟输入实战——输入文本与模拟按键的区别

影刀RPA新手教程:键盘模拟输入实战——输入文本与模拟按键的区别 做影刀RPA自动化,十个新手有八个栽在"往输入框里填东西"这件事上:要么填不进去,要么填了一半,要么直接把原来内容追加在后面。这背后的根因&…

2026/10/15 19:41:51 阅读更多 →
影刀RPA新手教程:阅文起点小说数据采集实战——书籍信息与章节内容

影刀RPA新手教程:阅文起点小说数据采集实战——书籍信息与章节内容

影刀RPA新手教程:阅文起点小说数据采集实战——书籍信息与章节内容 1. 认识影刀:什么场景该用RPA采小说数据 起点中文网的页面结构相对稳定——分类榜单、书籍详情、章节内容三块独立页面,跳转链路清晰。这种场景非常适合影刀自动化&#x…

2026/10/15 13:48:15 阅读更多 →

月新闻

我发现了一个新思路:用 Remotion + Claude Code 像写代码一样自动化生成短视频

我发现了一个新思路:用 Remotion + Claude Code 像写代码一样自动化生成短视频

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

2026/10/15 0:14:36 阅读更多 →
Windows下 Codex 中 Chrome 和 Computer Use 插件不可用问题排查及解决参考方式:TaoToken 统一 Key 配置与验证

Windows下 Codex 中 Chrome 和 Computer Use 插件不可用问题排查及解决参考方式:TaoToken 统一 Key 配置与验证

/* 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 3:49:43 阅读更多 →
黑夜航拍船只数据集训练YOLOV5模型全流程解析

黑夜航拍船只数据集训练YOLOV5模型全流程解析

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

2026/10/15 9:49:14 阅读更多 →