LC 963. 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.
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.

这题也卡住了,主要是如何判断平面上4个点是矩形?

可以先判断它是个平行四边形,然后判断它的一个角是90度。

x0,x1,x2,x3y0,y1,y2,y3

首先在不知道顺序的情况下,需要用全部的次序遍历。就是说1,2,3,4; 1,2,4,3;1,4,2,3;1,4,3,2的全排列。

其次判断直角。
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)

class Solution {
public:
 double minAreaFreeRect(vector<vector<int>>& points) {
   unordered_map<int, unordered_set<int>> c;
   for (auto &x:points)c[x[0]].insert(x[1]);
   long n = points.size(), x0, y0, x1, y1,x2,y2,x3,y3, r = LONG_MAX;
   REP(i, n) {
     x0 = points[i][0]; y0 = points[i][1];
     REP(j, n) {
       x1 = points[j][0]; y1 = points[j][1];
       REP(k, n)
       if (k != i && k != j) {
         x2 = points[k][0]; y2 = points[k][1];
         REP(l, n)
         if (l != i && l != j && l != k) {
           x3 = points[l][0]; y3 = points[l][1];
           if (x1-x0==x2-x3 && y1-y0==y2-y3 && x3-x0==x2-x1 && y3-y0==y2-y1)
             if ((x1-x0)*(x3-x0)+(y1-y0)*(y3-y0)==0)
               r = min(r, abs((x1 - x0) * (y3 - y0) - (y1 - y0) * (x3 - x0)));
         }
       }
     }
   }
   return r == LONG_MAX ? 0 : r;
 }

};


原文地址:https://www.cnblogs.com/ethanhong/p/10165825.html

时间: 2024-08-30 17:57:41

LC 963. Minimum Area Rectangle II的相关文章

LeetCode 963. Minimum Area Rectangle II

原题链接在这里:https://leetcode.com/problems/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

[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]]

LeetCode 939. Minimum Area Rectangle

原题链接在这里:https://leetcode.com/problems/minimum-area-rectangle/ 题目: Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes. If there isn't any rectangle, return

LeetCode - Minimum Area Rectangle

Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes. If there isn't any rectangle, return 0. Example 1: Input: [[1,1],[1,3],[3,1],[3,3],[2,2]] Output: 4 Ex

【leetcode】939. Minimum Area Rectangle

题目如下: Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes. If there isn't any rectangle, return 0. Example 1: Input: [[1,1],[1,3],[3,1],[3,3],[2,2]] Output

[LC] 76. Minimum Window Substring

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). Example: Input: S = "ADOBECODEBANC", T = "ABC" Output: "BANC" Note: If there is no such window in S

[LC] 452. Minimum Number of Arrows to Burst Balloons

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s

[LC] 1007. Minimum Domino Rotations For Equal Row

In a row of dominoes, A[i] and B[i] represent the top and bottom halves of the i-th domino.  (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.) We may rotate the i-th domino, so that A[i] and B[i] swap values. Return th

codeforces-1027 C Minimum Value Rectangle

1 #include <iostream> 2 #include <unordered_map> 3 #include <algorithm> 4 #include <vector> 5 #include <queue> 6 #define DEF 0x3f3f3f3f 7 using namespace std; 8 9 int main() 10 { 11 int T; 12 scanf("%d",&T); 13