[Swift]LeetCode587. 安装栅栏 | Erect the Fence

There are some trees, where each tree is represented by (x,y) coordinate in a two-dimensional garden. Your job is 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. Your task is to help find the coordinates of trees which are exactly located on the fence perimeter.

Example 1:

Input: [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]]
Output: [[1,1],[2,0],[4,2],[3,3],[2,4]]
Explanation:

Example 2:

Input: [[1,2],[2,2],[4,2]]
Output: [[1,2],[2,2],[4,2]]
Explanation:

Even you only have trees in a line, you need to use rope to enclose them. 

Note:

  1. All trees should be enclosed together. You cannot cut the rope to enclose trees that will separate them in more than one group.
  2. All input integers will range from 0 to 100.
  3. The garden has at least one tree.
  4. All coordinates are distinct.
  5. Input points have NO order. No order required for output.


在一个二维的花园中,有一些用 (x, y) 坐标表示的树。由于安装费用十分昂贵,你的任务是先用最短的绳子围起所有的树。只有当所有的树都被绳子包围时,花园才能围好栅栏。你需要找到正好位于栅栏边界上的树的坐标。

示例 1:

输入: [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]]
输出: [[1,1],[2,0],[4,2],[3,3],[2,4]]
解释:

示例 2:

输入: [[1,2],[2,2],[4,2]]
输出: [[1,2],[2,2],[4,2]]
解释:

即使树都在一条直线上,你也需要先用绳子包围它们。

注意:

  1. 所有的树应当被围在一起。你不能剪断绳子来包围树或者把树分成一组以上。
  2. 输入的整数在 0 到 100 之间。
  3. 花园至少有一棵树。
  4. 所有树的坐标都是不同的。
  5. 输入的点没有顺序。输出顺序也没有要求。


Runtime: 292 ms

Memory Usage: 20.1 MB

 1 /**
 2  * Definition for a point.
 3  * public class Point {
 4  *   public var x: Int
 5  *   public var y: Int
 6  *   public init(_ x: Int, _ y: Int) {
 7  *     self.x = x
 8  *     self.y = y
 9  *   }
10  * }
11  */
12 class Solution {
13     func outerTrees(_ points: [Point]) -> [Point] {
14         var res:[Point] = [Point]()
15         var first:Point = points[0]
16         var firstIdx:Int = 0
17         var n:Int = points.count
18         for i in 1..<n
19         {
20             if points[i].x < first.x
21             {
22                 first = points[i]
23                 firstIdx = i
24             }
25         }
26         res.append(first)
27         var cur:Point = first
28         var curIdx:Int = firstIdx
29         while(true)
30         {
31             var next:Point = points[0]
32             var nextIdx:Int = 0
33             for i in 1..<n
34             {
35                 if i == curIdx {continue}
36                 var cross:Int = crossProduct(cur, points[i], next)
37                 if nextIdx == curIdx || cross > 0 || (cross == 0 && dist(points[i], cur) > dist(next, cur))
38                 {
39                     next = points[i]
40                     nextIdx = i
41                 }
42             }
43             for i in 0..<n
44             {
45                 if i == curIdx {continue}
46                 var cross:Int = crossProduct(cur, points[i], next)
47                 if cross == 0
48                 {
49                     if check(&res, points[i])
50                     {
51                         res.append(points[i])
52                     }
53                 }
54             }
55             cur = next
56             curIdx = nextIdx
57             if curIdx == firstIdx {break}
58         }
59         return res
60     }
61
62     func crossProduct(_ A:Point,_ B:Point,_ C:Point) -> Int
63     {
64         var BAx:Int = A.x - B.x;
65         var BAy:Int = A.y - B.y;
66         var BCx:Int = C.x - B.x;
67         var BCy:Int = C.y - B.y;
68         return BAx * BCy - BAy * BCx;
69     }
70
71     func dist(_ A:Point,_ B:Point) -> Int
72     {
73         return (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y)
74     }
75
76     func check(_ res:inout [Point],_ p:Point) -> Bool
77     {
78         for r in res
79         {
80             if r.x == p.x && r.y == p.y {return false}
81         }
82         return true
83     }
84 }

原文地址:https://www.cnblogs.com/strengthen/p/10440054.html

时间: 2024-08-10 20:22:49

[Swift]LeetCode587. 安装栅栏 | Erect the Fence的相关文章

洛谷 P2205 [USACO13JAN]画栅栏Painting the Fence

P2205 [USACO13JAN]画栅栏Painting the Fence 题目描述 Farmer John has devised a brilliant method to paint the long fence next to his barn (think of the fence as a one-dimensional number line). He simply attaches a paint brush to his favorite cow Bessie, and t

openstack成长之旅 - 5 Swift介绍安装及作者的反思

这么长时间没有更新博客了,哎,之前都是用Ubuntu物理机搭建的openstack,但是最近用自己的centos虚拟机搭建的时候总是报错,一些基本的东西,频频出错,对此很是无奈啊,看来我的能力还是有待提升了,虽说虚拟机搭建openstack会有一些问题,但是也没有我的这么夸张吧,所以我决定,最近一段时间内要闭关修炼了,一定要能在任何环境中熟练操作openstack,希望大家能持续关注我哦. 下面简单讲解下Swift这个在openstack中起到非常重要的存储组件吧. Swift是openstac

Leetcode 587.安装栅栏

安装栅栏 在一个二维的花园中,有一些用 (x, y) 坐标表示的树.由于安装费用十分昂贵,你的任务是先用最短的绳子围起所有的树.只有当所有的树都被绳子包围时,花园才能围好栅栏.你需要找到正好位于栅栏边界上的树的坐标. 示例 1: 输入: [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]] 输出: [[1,1],[2,0],[4,2],[3,3],[2,4]] 解释: 示例 2: 输入: [[1,2],[2,2],[4,2]] 输出: [[1,2],[2,2],[4,2]]

openstack swift节点安装手册3-最后的安装配置及验证

以下步骤都在controller节点上执行 1.远程获取/etc/swift/swift.conf文件: curl -o /etc/swift/swift.conf https://git.openstack.org/cgit/openstack/swift/plain/etc/swift.conf-sample?h=stable/newton 2.修改/etc/swift/swift.conf配置文件: [swift-hash] ... swift_hash_path_suffix = HAS

openstack swift节点安装手册1-节点配置

本文参照官方教程:http://docs.openstack.org/project-install-guide/object-storage/draft/environment-networking.html 我们要设置的swift节点名称为object1,主控节点和proxy节点都是controller. 在controller节点的/etc/hosts文件里写上节点名称和ip地址,必须是能ping通的地址,同理,swift节点的/etc/hosts文件要写上各种地址. 在controlle

openstack swift节点安装手册2-创建rings

以下步骤需要在controller节点上进行操作: 切换到/etc/swift目录下进行如下操作: 一.创建account ring 1.创建account.builder文件 swift-ring-builder account.builder create 10 3 1 2.把每个节点添加到ring中,命令如下,有几台设备就重复几次,如有多台机器,第二胎机器创建时可以region相同,但zone必须换别的编号 swift-ring-builder account.builder add --

587. Erect the Fence

Problem statement: There are some trees, where each tree is represented by (x,y) coordinate in a two-dimensional garden. Your job is to fence the entire garden using the minimum length of rope as it is expensive. The garden is well fenced only if all

587. Erect the Fence(凸包算法)

问题 给定一群树的坐标点,画个围栏把所有树围起来(凸包). 至少有一棵树,输入和输出没有顺序. Input: [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]] Output: [[1,1],[2,0],[4,2],[3,3],[2,4]] 思路和代码 1. 暴力法(超时) 对于任意两点连成的一条直线,如果其它所有点都在这条直线的一侧,则这两个点为解集中的两个点. 怎么判断点在直线的同一侧呢? 假设确定直线的两点为p1(x1, y1)和p2(x2, y2),方向从p1到p

OpenStack之swift安装笔记

在顺利的安装部署了KeyStone之后,原以为swift的安装调试也会如KeyStone一般,但过程却充满了坎坷,在结合源代码中的一些函数之后,终于成功的完成了swift的安装,并实验了上传下载文件等功能,下面整理了swift的安装过程,并对遇到的问题进行了总结,希望对遇到同样问题的人有些启示或帮助.安装过程依然参考的是OpenStack的官方安装手册,但补充了一些细节.在安装之前,先简单的介绍一下Swift是什么.主要功能是什么.OpenStack的对象存储(Swift)是一个多租户的.高可扩