查看: 876| 回复: 0
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] 利口 就留三

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
这道题类似于脑筋急转弯啊,最优解就是,利用矩形的两个对角线长度必然相等的特性,根据对角线长和中点位置做哈希存储。

  1. class Solution {
  2. private:
  3.     unordered_map<string,vector<vector<int>>> mp;
  4. public:
  5.     double minAreaFreeRect(vector<vector<int>>& points) {
  6.         int n=points.size();
  7.         for(int i=0;i<n;i++)
  8.         {
  9.             for(int j=i+1;j<n;j++)
  10.             {
  11.                 int dx=points[i][0]-points[j][0];
  12.                 int dy=points[i][1]-points[j][1];
  13.                 long dist2=dx*dx+dy*dy;
  14.                 double cx=(points[i][0]+points[j][0])*0.5;
  15.                 double cy=(points[i][1]+points[j][1])*0.5;
  16.                 string s=to_string(dist2)+": "+to_string(cx)+", "+to_string(cy);
  17.                 mp[s].push_back({i,j});
  18.             }
  19.         }
  20.         double min_area=1e15;
  21.         for(auto it=mp.begin();it!=mp.end();it++)
  22.         {
  23.             if(it->second.size()<2) continue;
  24.             //there could be multiple rectangle inside
  25.             //now we have 4 points in diagonal, p0-p2 diagonal, p1-p3 diagonal
  26.             vector<vector<int>>& vrect=it->second;
  27.             for(int i=0;i<vrect.size();i++)
  28.             {
  29.                 for(int j=i+1;j<vrect.size();j++)
  30.                 {
  31.                     int p0=vrect[i][0],p2=vrect[i][1];
  32.                     int p1=vrect[j][0],p3=vrect[j][1];
  33.                     int dx=points[p0][0]-points[p1][0];
  34.                     int dy=points[p0][1]-points[p1][1];
  35.                     long long dist01=dx*dx+dy*dy;
  36.                     dx=points[p0][0]-points[p3][0];
  37.                     dy=points[p0][1]-points[p3][1];
  38.                     long long dist03=dx*dx+dy*dy;
  39.                     double area=sqrt(double(dist01)*dist03);
  40.                     min_area=min(min_area,area);
  41.                 }
  42.             }
  43.         }
  44.         return min_area==1e15?0:min_area;
  45.     }
  46. };
复制代码




补充内容 (2018-12-24 08:59):
求加米!

评分

参与人数 1大米 +5 收起 理由
xh_pku + 5 给你点个赞!

查看全部评分


上一篇:Amazon LC Dec.23 高頻題
下一篇:Island city 我的解法不对,百思不得其解
您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表