码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode每日一题(587. Erect the Fence)


    You are given an array trees where trees[i] = [xi, yi] represents the location of a tree in the garden.

    You are asked to fence the entire garden using the minimum length of rope as it is expensive. The garden is well fenced only if all the trees are enclosed.

    Return the coordinates of trees that are exactly located on the fence perimeter.

    Example 1:

    Input: points = [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]]
    Output: [[1,1],[2,0],[3,3],[2,4],[4,2]]

    Example 2:

    Input: points = [[1,2],[2,2],[4,2]]
    Output: [[4,2],[2,2],[1,2]]

    Constraints:

    • 1 <= points.length <= 3000
    • points[i].length == 2
    • 0 <= xi, yi <= 100
    • All the given points are unique.

    抄的 Jarvis Algorithm, 注意两点, orientation(p, q, r)因为是向量计算, 所以参数顺序很重要, 不要搞混了, 再就是官方的答案会导致死循环, 需要在共线检查里添加是否越过起始点的检查。剩下的大家看官方的解答吧


    
    use std::collections::HashSet;
    
    impl Solution {
        fn orientation(p: &[i32], q: &[i32], r: &[i32]) -> i32 {
            (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1])
        }
    
        fn in_between(p: &[i32], i: &[i32], q: &[i32]) -> bool {
            let a = i[0] <= p[0] && i[0] >= q[0] || i[0] >= p[0] && i[0] <= q[0];
            let b = i[1] <= p[1] && i[1] >= q[1] || i[1] >= p[1] && i[1] <= q[1];
            a && b
        }
        pub fn outer_trees(trees: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
            if trees.len() < 4 {
                return trees;
            }
            let mut hull = HashSet::new();
            let mut left_most = 0;
            for i in 0..trees.len() {
                if trees[i][0] < trees[left_most][0] {
                    left_most = i;
                }
            }
            hull.insert(left_most);
            let mut p = left_most;
            loop {
                let mut q = (p + 1) % trees.len();
                for i in 0..trees.len() {
                    if Solution::orientation(&trees[p], &trees[q], &trees[i]) > 0 {
                        q = i;
                    }
                }
                hull.insert(q);
                let mut reached_begin = false;
                for i in 0..trees.len() {
                    if i != p
                        && i != q
                        && Solution::orientation(&trees[p], &trees[q], &trees[i]) == 0
                        && Solution::in_between(&trees[p], &trees[i], &trees[q])
                    {
                        if i == left_most {
                            reached_begin = true;
                        }
                        hull.insert(i);
                    }
                }
                p = q;
                if p == left_most || reached_begin {
                    break;
                }
            }
            trees
                .into_iter()
                .enumerate()
                .filter(|(i, _)| hull.contains(&i))
                .map(|(_, v)| v)
                .collect()
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
  • 相关阅读:
    算法学习——贪心算法
    软件测试流程分享
    【机器学习基础】 线性回归
    MySQL基础学习总结(三)
    基于FPGA的图像一维FFT变换IFFT逆变换verilog实现,包含tb测试文件和MATLAB辅助验证
    Vue3.0 +Quasar+ ethers.js 和以太坊智能合约交互
    关于文件上传的前后端优化
    构建创新增值能力优势,康铂酒店突围中端酒店市场!
    【毕业设计】基于javaEE+SSH+SQL Server的学生信息管理系统设计与实现(毕业论文+程序源码)——学生信息管理系统
    机器学习:PCA(Principal Component Analysis主成分)降维
  • 原文地址:https://blog.csdn.net/wangjun861205/article/details/127934543
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号