[Swift Weekly Contest 116]LeetCode963. 最小面积矩形 II | Minimum Area Rectangle II

Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these points, with sides not necessarily parallel to the x and y axes.

If there isn‘t any rectangle, return 0.

Example 1:

Input: [[1,2],[2,1],[1,0],[0,1]]
Output: 2.00000
Explanation: The minimum area rectangle occurs at [1,2],[2,1],[1,0],[0,1], with an area of 2.

Example 2:

Input: [[0,1],[2,1],[1,1],[1,0],[2,0]]
Output: 1.00000
Explanation: The minimum area rectangle occurs at [1,0],[1,1],[2,1],[2,0], with an area of 1.

Example 3:

Input: [[0,3],[1,2],[3,1],[1,3],[2,1]]
Output: 0
Explanation: There is no possible rectangle to form from these points.

Example 4:

Input: [[3,1],[1,1],[0,1],[2,1],[3,3],[3,2],[0,2],[2,3]]
Output: 2.00000
Explanation: The minimum area rectangle occurs at [2,1],[2,3],[3,3],[3,1], with an area of 2.

Note:

  1. 1 <= points.length <= 50
  2. 0 <= points[i][0] <= 40000
  3. 0 <= points[i][1] <= 40000
  4. All points are distinct.
  5. Answers within 10^-5 of the actual value will be accepted as correct.


给定在 xy 平面上的一组点,确定由这些点组成的任何矩形的最小面积,其中矩形的边不一定平行于 x 轴和 y 轴。

如果没有任何矩形,就返回 0。

示例 1:

输入:[[1,2],[2,1],[1,0],[0,1]]
输出:2.00000
解释:最小面积的矩形出现在 [1,2],[2,1],[1,0],[0,1] 处,面积为 2。

示例 2:

输入:[[0,1],[2,1],[1,1],[1,0],[2,0]]
输出:1.00000
解释:最小面积的矩形出现在 [1,0],[1,1],[2,1],[2,0] 处,面积为 1。

示例 3:

输入:[[0,3],[1,2],[3,1],[1,3],[2,1]]
输出:0
解释:没法从这些点中组成任何矩形。

示例 4:

输入:[[3,1],[1,1],[0,1],[2,1],[3,3],[3,2],[0,2],[2,3]]
输出:2.00000
解释:最小面积的矩形出现在 [2,1],[2,3],[3,3],[3,1] 处,面积为 2。

提示:

  1. 1 <= points.length <= 50
  2. 0 <= points[i][0] <= 40000
  3. 0 <= points[i][1] <= 40000
  4. 所有的点都是不同的。
  5. 与真实值误差不超过 10^-5 的答案将视为正确结果。


460ms

 1 class Solution {
 2     func minAreaFreeRect(_ points: [[Int]]) -> Double {
 3         var ans:Double = -1
 4         var set:Set<Int> = Set<Int>()
 5         for p in points
 6         {
 7             set.insert((p[0] << 16) | p[1])
 8         }
 9
10         var n:Int = points.count
11         for i in 0..<n
12         {
13             for j in 0..<n
14             {
15                 var x0:Int = points[j][0] - points[i][0]
16                 var y0:Int = points[j][1] - points[i][1]
17                 if i != j
18                 {
19                     for k in 0..<n
20                     {
21                         if i != k && j != k
22                         {
23                             var x1:Int = points[k][0] - points[i][0]
24                             var y1:Int = points[k][1] - points[i][1]
25                             if x0 * x1 + y0 * y1 == 0
26                             {
27                                 var x:Int = points[j][0] + points[k][0] - points[i][0]
28                                 var y:Int = points[j][1] + points[k][1] - points[i][1]
29                                 if set.contains((x << 16) | y)
30                                 {
31                                     var cur:Double = sqrt(Double(x0 * x0 + y0 * y0)) * sqrt(Double(x1 * x1 + y1 * y1))
32                                     if ans < 0 || ans > cur
33                                     {
34                                         ans = cur
35                                     }
36                                 }
37                             }
38                         }
39                     }
40                 }
41             }
42         }
43         return ans < 0 ? 0 : ans
44     }
45 }

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

时间: 2024-07-31 21:28:13

[Swift Weekly Contest 116]LeetCode963. 最小面积矩形 II | Minimum Area Rectangle II的相关文章

[Swift Weekly Contest 116]LeetCode961. 重复 N 次的元素 | N-Repeated Element in Size 2N Array

In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeated N times. Return the element repeated N times. Example 1: Input: [1,2,3,3] Output: 3 Example 2: Input: [2,1,2,5,3,2] Output: 2 Example 3: Input: [5,1

LeetCode939 最小面积矩形

LeetCode939最小面积矩形 给定在 xy 平面上的一组点,确定由这些点组成的矩形的最小面积,其中矩形的边平行于 x 轴和 y 轴. 如果没有任何矩形,就返回 0. Input [[1,1],[1,3],[3,1],[3,3],[2,2]] Output 4 hint 1 <= points.length <= 500 0 <= points[i][0] <= 40000 0 <= points[i][1] <= 40000 所有的点都是不同的. 题目大意 给许多

[Swift Weekly Contest 108]LeetCode931. 下降路径最小和 | Minimum Falling Path Sum

Given a square array of integers A, we want the minimum sum of a falling path through A. A falling path starts at any element in the first row, and chooses one element from each row.  The next row's choice must be in a column that is different from t

[Swift Weekly Contest 109]LeetCode934. 最短的桥 | Shortest Bridge

In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected group of 1s not connected to any other 1s.) Now, we may change 0s to 1s so as to connect the two islands together to form 1 island. Return the smallest nu

[Swift Weekly Contest 111]LeetCode944. 删除列以使之有序 | Delete Columns to Make Sorted

We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have a string "abcdef" and dele

[Swift Weekly Contest 113]LeetCode952. 按公因数计算最大组件大小 | Largest Component Size by Common Factor

Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.

[Swift Weekly Contest 115]LeetCode960. 删列造序 ||| | Delete Columns to Make Sorted III

We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have an array A = ["babca",&quo

[Swift Weekly Contest 118]LeetCode976. 三角形的最大周长 | Largest Perimeter Triangle

Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, formed from 3 of these lengths. If it is impossible to form any triangle of non-zero area, return 0. Example 1: Input: [2,1,2] Output: 5 Example 2: I

[Swift Weekly Contest 119]LeetCode975. 奇偶跳 | Odd Even Jump

You are given an integer array A.  From some starting index, you can make a series of jumps.  The (1st, 3rd, 5th, ...) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, ...) jumps in the series are called even numbered jumps.