Skip to content

Commit

Permalink
update ws
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Sep 29, 2024
1 parent 58c9629 commit 7c8aba8
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,59 @@ private int getDays(List<Integer> weightsList, int speed){
*
*
*
* idea :
* idea 1:
* get min "x len"
* and get min "y len"
* then ans = x_len * y_len
*
* idea 2 :
*
* hash map
*
* {"x": [y1, y2,,,],
* "y" : [x1, x2,,,,]
* }
*
*/
public int minAreaRect(int[][] points) {

return 0;
// build map
Map<Integer, List<Integer>> mapX = new HashMap<>();
Map<Integer, List<Integer>> mapY = new HashMap<>();

for (int[] point : points){

int x = point[0];
int y = point[1];

List<Integer> xList = mapX.getOrDefault(x, new ArrayList<>());
List<Integer> yList = mapY.getOrDefault(x, new ArrayList<>());

xList.add(x);
yList.add(y);

mapX.put(x, xList);
mapY.put(y, yList);
}

int res = Integer.MAX_VALUE;

for (int i = 0; i < points.length; i++){
for (int j = 0; j <points.length; j++){
int x1 = points[i][0];
int y1 = points[i][1];
int x2 = points[j][0];
int y2 = points[j][1];
// check
boolean canFormRectangle = (x1 != x2 && y1 != y2 && mapX.containsKey(x1) && mapX.containsKey(x2) && mapX.get(x1).equals(y2) && mapX.get(x2).equals(y1));
if (canFormRectangle){
int area = (x2 - x1) * (y2 - y1);
res = Math.min(res, area);
}
}
}

return res == Integer.MAX_VALUE ? 0 : res;
}

// public int minAreaRect(int[][] points) {
Expand Down

0 comments on commit 7c8aba8

Please sign in to comment.