楼主: shu16
跳转到指定楼层
上一主题 下一主题
收起左侧

狗家实习电面面经,感觉差不多跪了,来攒人品……

🔗
lf963 2018-4-7 07:25:53 | 只看该作者
全局:
amazinglu 发表于 2018-3-5 02:45
第一题 O(n^2) 就可以

找到左上方的点(x1, y1) 和 右下方的点 (x2, y2)

pointSet的type不是Point[]嗎?
請問pointSet.x代表甚麼?
回复

使用道具 举报

🔗
lf963 2018-4-7 07:54:31 | 只看该作者
全局:
给一串数组代表坐标,求问这些坐标中能连成的长方形中,面积最大的那个是多大

假設長方行的邊皆與X軸或Y軸垂直或平行,不會有斜的長方形
  1. import java.util.*;
  2. public class RectangleMaxArea {
  3.     public static void main(String[] args){
  4.         Coordinate[] coordinates= new Coordinate[10];
  5.         coordinates[0] = new Coordinate(-1,5);
  6.         coordinates[1] = new Coordinate(-1,-3);
  7.         coordinates[2] = new Coordinate(2,7);
  8.         coordinates[3] = new Coordinate(2,3);
  9.         coordinates[4] = new Coordinate(4,-1);
  10.         coordinates[5] = new Coordinate(6,7);
  11.         coordinates[6] = new Coordinate(6,3);
  12.         coordinates[7] = new Coordinate(7,2);
  13.         coordinates[8] = new Coordinate(2,-4);
  14.         coordinates[9] = new Coordinate(6,-4);
  15.         System.out.println(new RectangleMaxArea().getMaxArea(coordinates));
  16.     }

  17.     int getMaxArea(Coordinate[] coordinates){
  18.         int maxArea = 0;
  19.         Set<Coordinate> mySet = new HashSet<>();
  20.         Collections.addAll(mySet,coordinates);
  21.         if(mySet.size() < 4)
  22.             return 0;
  23.         int pointNum = coordinates.length;

  24.         //we want to pick two diagonal points
  25.         for(int i=0; i<pointNum; i++){
  26.             for(int j=0; j<pointNum; j++){
  27.                 // if we pick two points with
  28.                 // 1. same coordinates or
  29.                 // 2. they are on the same vertical line or
  30.                 // 3. they are on the same horizontal line
  31.                 // we skip them because we want to pick diagonal point of the rectangle
  32.                 if(coordinates[i].equals(coordinates[j])
  33.                         || coordinates[i].x == coordinates[j].x
  34.                         || coordinates[i].y == coordinates[j].y)
  35.                     continue;
  36.                 int[] p1 = {coordinates[i].x, coordinates[i].y};
  37.                 int[] p2 = {coordinates[j].x, coordinates[j].y};
  38.                 int[] p3 = new int[2];
  39.                 int[] p4 = new int[2];

  40.                 if(mySet.contains(new Coordinate(p2[0],p1[1])) && mySet.contains(new Coordinate(p1[0],p2[1]))){
  41.                     p3[0] = p2[0];
  42.                     p3[1] = p1[1];
  43.                     p4[0] = p1[0];
  44.                     p4[1] = p2[1];
  45.                     maxArea = Math.max(maxArea,getArea(p1,p3,p4));
  46.                 }
  47.             }
  48.         }

  49.         return maxArea;
  50.     }

  51.     int getArea(int[] p1, int[] p2, int[] p3){
  52.         int edge1 = p1[0]-p2[0] + p1[1]-p2[1];
  53.         int edge2 = p1[0]-p3[0] + p1[1]-p3[1];
  54.         return edge1 * edge2;
  55.     }
  56. }

  57. class Coordinate{
  58.     int x;
  59.     int y;
  60.     Coordinate(int x, int y){
  61.         this.x = x;
  62.         this.y = y;
  63.     }

  64.     public boolean equals(Object p){
  65.         Coordinate c = (Coordinate)p;
  66.         return c.x == this.x && c.y == this.y;
  67.     }

  68.     public int hashCode(){
  69.         final int prime = 997;
  70.         return x + prime * y;
  71.     }
  72. }
复制代码
回复

使用道具 举报

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

本版积分规则

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