description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
S = set(map(tuple, points))
ans = float("inf")
for j, p2 in enumerate(points):
for i in range(j):
p1 = points[i]
if (
p1[0] != p2[0]
and p1[1] != p2[1]
and (p1[0], p2[1]) in S
and (p2[0], p1[1]) in S
):
ans = min(ans, abs(p2[0] - p1[0]) * abs(p2[1] - p1[1]))
return ans if ans < float("inf") else 0
columns = collections.defaultdict(list)
for x, y in points:
columns[x].append(y)
lastx = {}
ans = float("inf")
for x in sorted(columns):
column = columns[x]
column.sort()
for j, y2 in enumerate(column):
for i in range(j):
y1 = column[i]
if (y1, y2) in lastx:
ans = min(ans, (x - lastx[y1, y2]) * (y2 - y1))
lastx[y1, y2] = x
return ans if ans < float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
s = set([(x, y) for x, y in points])
res = float("inf")
for i, point1 in enumerate(points):
for j in range(i):
point2 = points[j]
x1, y1 = point1
x2, y2 = point2
if x1 != x2 and y1 != y2 and (x1, y2) in s and (x2, y1) in s:
res = min(res, abs(x1 - x2) * abs(y1 - y2))
return res if res < float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Point:
def __init__(self, x, y):
self.x = x
self.y = y
class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
points = [Point(x, y) for [x, y] in points]
dic_x = collections.defaultdict(set)
dic_y = collections.defaultdict(set)
for p in points:
dic_x[p.x].add(p.y)
dic_y[p.y].add(p.x)
area = math.inf
for p1, p2 in itertools.combinations(points, 2):
if p1.x == p2.x or p1.y == p2.y:
continue
if p2.y in dic_x[p1.x] and p2.x in dic_y[p1.y]:
area = min(area, abs(p2.x - p1.x) * abs(p2.y - p1.y))
return area if area != math.inf else 0 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR VAR VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
result = float("inf")
columns = collections.defaultdict(lambda: [])
for x, y in points:
columns[x].append(y)
print("columns is: " + str(columns))
lastSeen = {}
for x in sorted(columns.keys()):
column = sorted(columns[x])
for i in range(len(column)):
y1 = column[i]
for j in range(i + 1, len(column)):
if y1 != column[j]:
y2 = column[j]
if (y1, y2) in lastSeen:
prev_x = lastSeen[y1, y2]
result = min(result, (y2 - y1) * (x - prev_x))
lastSeen[y1, y2] = x
return 0 if result == float("inf") else result | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
d_x = collections.defaultdict(set)
d_y = collections.defaultdict(set)
for x, y in points:
d_x[x].add(y)
d_y[y].add(x)
area = sys.maxsize
for x, y in points:
for yy in d_x[x]:
if yy <= y:
continue
for xx in d_y[y]:
if xx > x and yy in d_x[xx]:
area = min(area, (xx - x) * (yy - y))
return area if area != sys.maxsize else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRectangularHull(self, points: List[List[int]]) -> int:
area = 0
if points:
minx, miny = points[0]
maxx, maxy = points[0]
for x, y in points:
minx = min(minx, x)
miny = min(miny, y)
maxx = max(maxx, x)
maxy = max(maxy, y)
area = (maxx - minx) * (maxy - miny)
return area
def minAreaRect(self, points):
S = set(map(tuple, points))
ans = float("inf")
for k, (xB, yB) in enumerate(points):
for j in range(k):
xA, yA = points[j]
if xA != xB and yA != yB and (xA, yB) in S and (xB, yA) in S:
ans = min(ans, abs((xB - xA) * (yB - yA)))
return ans if ans < float("inf") else 0
class Solution:
def minAreaRect(self, points):
columns = collections.defaultdict(set)
for x, y in points:
columns[x].add(y)
lastx = {}
ans = float("inf")
for x in sorted(columns):
column = sorted(columns[x])
for j, y2 in enumerate(column):
for i in range(j):
y1 = column[i]
if (y1, y2) in lastx:
ans = min(ans, (x - lastx[y1, y2]) * (y2 - y1))
lastx[y1, y2] = x
return ans if ans < float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
pointset = set([tuple(x) for x in points])
res = 2**32
for i in range(len(points)):
for j in range(i + 1, len(points)):
p1 = points[i]
p2 = points[j]
if p1[0] != p2[0] and p1[1] != p2[1]:
if (p1[0], p2[1]) in pointset and (p2[0], p1[1]) in pointset:
res = min(res, abs(p1[0] - p2[0]) * abs(p1[1] - p2[1]))
return res if res < 2**32 else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR BIN_OP NUMBER NUMBER VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | from itertools import combinations
class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
area = float("inf")
by_x = {}
visited = {}
for point in points:
by_x[point[0]] = []
for point in points:
by_x[point[0]].append(point[1])
for x in sorted(by_x):
for y1, y2 in combinations(sorted(by_x[x]), 2):
if (y1, y2) in visited:
area = min(area, abs(x - visited[y1, y2]) * abs(y2 - y1))
visited[y1, y2] = x
return area if area != float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
point_set = set(map(tuple, points))
min_area = float("inf")
for i, (x1, y1) in enumerate(points):
for x2, y2 in points[:i]:
if x1 != x2 and y1 != y2:
if (x1, y2) in point_set and (x2, y1) in point_set:
min_area = min(min_area, abs((x2 - x1) * (y2 - y1)))
return min_area if min_area != float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
if not points:
return 0
points_table = set()
for x, y in points:
points_table.add((x, y))
min_area = float("inf")
for i in range(len(points) - 1):
for j in range(i + 1, len(points)):
x1, y1 = points[i]
x2, y2 = points[j]
if x1 != x2 and y1 != y2:
if (x1, y2) in points_table and (x2, y1) in points_table:
min_area = min(min_area, abs(x1 - x2) * abs(y1 - y2))
return 0 if min_area == float("inf") else min_area | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points):
seen, ans = set(map(tuple, points)), float("inf")
for i, (p1x, p1y) in enumerate(points):
for p2x, p2y in points[i + 1 :]:
if p1x != p2x and p1y != p2y:
area = abs((p2x - p1x) * (p2y - p1y))
if area > ans or area == 0:
continue
if (p1x, p2y) in seen and (p2x, p1y) in seen:
ans = area
return ans if ans < float("inf") else 0 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
m = set()
for point in points:
m.add(tuple(point))
res = 1073741823
for i in range(len(points)):
x1, y1 = points[i]
for j in range(i + 1, len(points)):
x2, y2 = points[j]
if x1 != x2 and y1 != y2:
if (x1, y2) in m and (x2, y1) in m:
res = min(res, abs((y1 - y2) * (x1 - x2)))
res = 0 if res == 1073741823 else res
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER VAR RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
n = len(points)
if n < 4:
return 0
ptSet = set([(x, y) for x, y in points])
area = math.inf
for i in range(n):
for j in range(i):
if points[i][0] != points[j][0] and points[i][1] != points[j][1]:
if (points[i][0], points[j][1]) in ptSet and (
points[j][0],
points[i][1],
) in ptSet:
area = min(
area,
abs(points[i][0] - points[j][0])
* abs(points[i][1] - points[j][1]),
)
return area if area < math.inf else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER RETURN VAR VAR VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
points.sort()
n = len(points)
pmap = {(i, j) for i, j in points}
area = float("inf")
for i in range(n):
for j in range(i + 1, n):
x1 = points[i][0]
y1 = points[i][1]
x2 = points[j][0]
y2 = points[j][1]
if x1 != x2 and y2 != y1 and x1 != y2 and x2 != y1:
if (x1, y2) in pmap and (x2, y1) in pmap:
area = min(area, abs(x1 - x2) * abs(y2 - y1))
return 0 if area == float("inf") else area | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
horizontal = collections.defaultdict(set)
vertical = collections.defaultdict(set)
for x, y in points:
horizontal[y].add(x)
vertical[x].add(y)
xlist = [x for x in vertical if len(vertical[x]) > 1]
ans = float("inf")
for i in range(len(xlist) - 1):
x1 = xlist[i]
for j in range(i + 1, len(xlist)):
x2 = xlist[j]
cand = []
for y in vertical[x1]:
if y in vertical[x2]:
cand.append(y)
if len(cand) >= 2:
cand.sort()
for i in range(len(cand) - 1):
ans = min(ans, abs(x2 - x1) * (cand[i + 1] - cand[i]))
return ans if ans < float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
rows = defaultdict(set)
cols = defaultdict(set)
for i, point in enumerate(points):
rows[point[0]].add(tuple(point))
cols[point[1]].add(tuple(point))
minArea = 99999999999
for point in points:
rowPoints = rows[point[0]]
colPoints = cols[point[1]]
for iP in rowPoints:
for jP in colPoints:
if (
iP[1] > point[1]
and jP[0] > point[0]
and (jP[0], iP[1]) in rows[jP[0]]
):
minArea = min(minArea, (iP[1] - point[1]) * (jP[0] - point[0]))
if minArea == 99999999999:
return 0
return minArea | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
points_set = {tuple(p) for p in points}
res = float("inf")
for p1 in points:
for p2 in points:
if p1[0] != p2[0] and p1[1] != p2[1]:
area = abs(p1[0] - p2[0]) * abs(p1[1] - p2[1])
if area < res:
if (p1[0], p2[1]) in points_set and (
p2[0],
p1[1],
) in points_set:
res = area
return 0 if res == float("inf") else res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
res = float("inf")
pointSet = set([tuple(p) for p in points])
for i in range(len(points)):
for j in range(i + 1, len(points)):
x1, y1 = points[i]
x2, y2 = points[j]
if x1 == x2 or y1 == y2:
continue
if (x1, y2) in pointSet and (x2, y1) in pointSet:
res = min(res, abs(x1 - x2) * abs(y1 - y2))
if res == float("inf"):
return 0
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
st = set()
min_area = float("inf")
for item in points:
st.add((item[0], item[1]))
for i in range(len(points) - 1):
for j in range(i + 1, len(points)):
x1, y1 = points[i][0], points[i][1]
x2, y2 = points[j][0], points[j][1]
if x1 == x2 or y1 == y2:
continue
if (x1, y2) in st and (x2, y1) in st:
curr_area = abs((y2 - y1) * (x2 - x1))
min_area = min(min_area, curr_area)
if min_area == float("inf"):
return 0
return min_area | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
ptmap = {}
min_area = float("inf")
for x1, y1 in points:
for x2, y2 in ptmap:
if (x1, y2) in ptmap and (x2, y1) in ptmap:
area = abs(x2 - x1) * abs(y2 - y1)
min_area = min(area, min_area)
ptmap[x1, y1] = x1, y1
return min_area if min_area != float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
pointsSet = set([(x, y) for x, y in points])
points = list(pointsSet)
numberOfPoints = len(points)
minimumArea = float("inf")
for i in range(numberOfPoints):
for j in range(i + 1, numberOfPoints):
x1, y1 = points[i]
x2, y2 = points[j]
if (
x1 != x2
and y1 != y2
and (x1, y2) in pointsSet
and (x2, y1) in pointsSet
):
minimumArea = min(minimumArea, abs(x2 - x1) * abs(y2 - y1))
return 0 if minimumArea == float("inf") else minimumArea | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
points.sort()
xs = collections.defaultdict(list)
for x, y in points:
xs[x].append(y)
seen = {}
result = math.inf
for x, ylist in xs.items():
for ys in itertools.combinations(ylist, 2):
if ys in seen:
result = min(result, (x - seen[ys]) * (ys[1] - ys[0]))
seen[ys] = x
return 0 if math.isinf(result) else result | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
def calculateArea(x1, x2, y1, y2):
return abs(y2 - y1) * abs(x2 - x1)
if not points or len(points) < 4:
return 0
visited = set()
area = float("inf")
for x1, y1 in points:
for x2, y2 in visited:
if (x1, y2) in visited and (x2, y1) in visited:
area = min(area, calculateArea(x1, x2, y1, y2))
visited.add((x1, y1))
return area if area != float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
seen = set()
res = float("inf")
for point in points:
x, y = point[0], point[1]
for _x, _y in seen:
if (x, _y) in seen and (_x, y) in seen:
res = min(res, abs(x - _x) * abs(y - _y))
seen.add((x, y))
if res == float("inf"):
return 0
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
point_set = set((x, y) for x, y in points)
min_rec = math.inf
points.sort()
for idx1 in range(len(points)):
for idx2 in range(idx1 + 1, len(points)):
x1, y1 = points[idx1]
x2, y2 = points[idx2]
if x1 < x2 and y1 > y2:
if (x1, y2) in point_set and (x2, y1) in point_set:
min_rec = min(min_rec, (x2 - x1) * (y1 - y2))
return 0 if min_rec == math.inf else min_rec | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR VAR NUMBER VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
points = sorted(points, key=lambda x: (x[0], x[1]))
self.res = float("inf")
self.cache = defaultdict(list)
x_cache = defaultdict(list)
for x, y in points:
for y2 in x_cache[x]:
if y2 < y:
for x_other in self.cache[y, y2]:
self.res = min(self.res, abs(x_other - x) * abs(y2 - y))
self.cache[y, y2].append(x)
x_cache[x].append(y)
if self.res == float("inf"):
self.res = 0
return self.res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
counter = Counter(list(map(tuple, points)))
points = list(counter)
result = float("inf")
for i, (x, y) in enumerate(points):
for j in range(i + 1, len(points)):
x_, y_ = points[j]
if x != x_ and y != y_:
area = abs((x_ - x) * (y_ - y))
if area < result and (x, y_) in counter and (x_, y) in counter:
if not area:
return 0
result = area
elif counter[x, y] == 2 and counter[x_, y_] == 2:
return 0
return 0 if result == float("inf") else result | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
S = {(point[0], point[1]) for point in points}
minArea = math.inf
for i, point1 in enumerate(points):
for j in range(i):
point2 = points[j]
x1, x2, y1, y2 = point1[0], point2[0], point1[1], point2[1]
diagPoints = x1 != x2 and y1 != y2
if diagPoints and (x1, y2) in S and (x2, y1) in S:
minArea = min(minArea, abs(x1 - x2) * abs(y1 - y2))
return minArea if minArea != math.inf else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR VAR VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
sorted_points = {}
for point in points:
if point[0] not in sorted_points:
sorted_points[point[0]] = []
sorted_points[point[0]].append(point)
sorted_col = sorted(sorted_points.keys())
min_area = None
seen = {}
for col in sorted_col:
sorted_points[col] = sorted(sorted_points[col])
for idx1 in range(len(sorted_points[col])):
for idx2 in range(idx1 + 1, len(sorted_points[col])):
point1 = sorted_points[col][idx1]
point2 = sorted_points[col][idx2]
if (point1[1], point2[1]) in seen:
temp = (point2[1] - point1[1]) * (
col - seen[point1[1], point2[1]]
)
min_area = min(min_area, temp) if min_area is not None else temp
seen[point1[1], point2[1]] = col
return min_area if min_area is not None else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER LIST EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NONE ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NONE FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR RETURN VAR NONE VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
point_set = set()
area = 0
for i1, j1 in points:
for i2, j2 in points:
if i1 != i2 and j1 != j2:
if (i1, j2) in point_set and (i2, j1) in point_set:
if area == 0:
area = abs(i1 - i2) * abs(j1 - j2)
else:
area = min(area, abs(i1 - i2) * abs(j1 - j2))
point_set.add((i1, j1))
return area | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
p = collections.defaultdict(set)
for x, y in points:
p[x].add(y)
xs = sorted(p.keys())
minv = float("inf")
for i in range(len(xs) - 1):
for j in range(i + 1, len(xs)):
le = xs[j] - xs[i]
if le > minv:
break
potential = sorted(p[xs[i]].intersection(p[xs[j]]))
v = float("inf")
for j in range(len(potential) - 1):
v = min(v, potential[j + 1] - potential[j])
minv = min(minv, v * le)
return minv if minv < float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
n = len(points)
nx = len(set(x for x, _ in points))
ny = len(set(y for _, y in points))
if nx == n and ny == n:
return False
p = collections.defaultdict(list)
if nx > ny:
for x, y in points:
p[x].append(y)
else:
for x, y in points:
p[y].append(x)
last_x = {}
area = float("inf")
for x in sorted(p):
p[x].sort()
for i in range(len(p[x])):
for j in range(i):
y1, y2 = p[x][j], p[x][i]
if (y1, y2) in last_x:
area = min(area, (x - last_x[y1, y2]) * (y2 - y1))
last_x[y1, y2] = x
return area if area < float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
points = {(x, y) for x, y in points}
min_area = float("inf")
for x1, y1 in points:
for x2, y2 in points:
if x1 == x2 or y1 == y2:
continue
if abs((x1 - x2) * (y1 - y2)) > min_area:
continue
if (x1, y2) in points and (x2, y1) in points:
min_area = min(min_area, abs((x1 - x2) * (y1 - y2)))
if min_area == float("inf"):
return 0
return min_area | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | import itertools
class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
if not points:
return 0
ybucket = defaultdict(list)
for x, y in points:
ybucket[y].append(x)
for k, v in ybucket.items():
if len(v) >= 2:
ybucket[k] = sorted(v)
ys = [k for k in ybucket.keys() if len(ybucket[k]) >= 2]
ans = float("inf")
for y1, y2 in itertools.combinations(ys, 2):
row1 = set(ybucket[y1])
row2 = ybucket[y2]
dy = abs(y2 - y1)
xlast = None
for x in row2:
if x in row1:
if xlast is not None:
ans = min(ans, abs(x - xlast) * dy)
xlast = x
return ans if ans < float("inf") else 0 | IMPORT CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NONE FOR VAR VAR IF VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
point_set = set()
x_map, y_map = defaultdict(set), defaultdict(set)
for x, y in points:
point_set.add((x, y))
x_map[x].add((x, y))
y_map[y].add((x, y))
min_rec = math.inf
def dfs(points: list):
nonlocal x_map, y_map, point_set, min_rec
if len(points) not in (1, 2, 3):
return
if len(points) == 1:
x, y = points[0]
for point in x_map[x]:
if point[1] < y:
points.append(point)
dfs(points)
points.pop()
elif len(points) == 2:
x, y = points[0]
for point in y_map[y]:
if point[0] > x:
points.append(point)
dfs(points)
points.pop()
else:
x2, y1 = points[2][0], points[1][1]
if (x2, y1) in point_set:
x1, y2 = points[1][0], points[2][1]
min_rec = min(min_rec, (x2 - x1) * (y2 - y1))
for point in point_set:
dfs([point])
if min_rec != math.inf:
return min_rec
else:
return 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_DEF VAR IF FUNC_CALL VAR VAR NUMBER NUMBER NUMBER RETURN IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR IF VAR VAR RETURN VAR RETURN NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
all_points = set([(point[0], point[1]) for point in points])
min_area = float("inf")
for idx, point1 in enumerate(points):
for idx, point2 in enumerate(points[idx + 1 :], start=idx):
if (
point1[0] != point2[0]
and point1[1] != point2[1]
and (point1[0], point2[1]) in all_points
and (point2[0], point1[1]) in all_points
):
min_area = min(
min_area,
abs(point1[0] - point2[0]) * abs(point1[1] - point2[1]),
)
return min_area if min_area != float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
min_area = sys.maxsize
point_set = set([(p[0], p[1]) for p in points])
for i in range(len(points)):
for j in range(i + 1, len(points)):
p1 = points[i]
p2 = points[j]
if p1[0] != p2[0] and p1[1] != p2[1]:
p3 = p1[0], p2[1]
p4 = p2[0], p1[1]
if p3 in point_set and p4 in point_set:
min_area = min(min_area, abs((p1[0] - p2[0]) * (p1[1] - p2[1])))
return min_area if min_area != sys.maxsize else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
res = float("inf")
lookup = set()
for x1, y1 in points:
for x2, y2 in lookup:
if (x1, y2) in lookup and (x2, y1) in lookup:
res = min(res, abs(x1 - x2) * abs(y1 - y2))
lookup.add((x1, y1))
return res if res != float("inf") else 0
ys_with_same_x = defaultdict(list)
for x, y in points:
ys_with_same_x[x].append(y)
pair_of_ys_history = {}
res = float("inf")
for x, ys in sorted(ys_with_same_x.items()):
ys.sort()
for i, y1 in enumerate(ys):
for j in range(i):
y2 = ys[j]
if (y1, y2) in pair_of_ys_history:
res = min(res, (x - pair_of_ys_history[y1, y2]) * (y1 - y2))
pair_of_ys_history[y1, y2] = x
if res == float("inf"):
return 0
else:
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
x = 0
y = 1
points_set = set()
for point in points:
points_set.add((point[x], point[y]))
if len(points_set) < 4:
return 0
best = None
for i in range(len(points) - 1):
for j in range(i + 1, len(points)):
p1 = points[i]
p2 = points[j]
if p1[x] == p2[x] or p1[y] == p2[y]:
continue
if (p1[x], p2[y]) in points_set and (p2[x], p1[y]) in points_set:
area = abs(p2[x] - p1[x]) * abs(p2[y] - p1[y])
best = area if best is None else min(best, area)
return 0 if not best else best | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NONE VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRectangularHull(self, points: List[List[int]]) -> int:
area = 0
if points:
minx, miny = points[0]
maxx, maxy = points[0]
for x, y in points:
minx = min(minx, x)
miny = min(miny, y)
maxx = max(maxx, x)
maxy = max(maxy, y)
area = (maxx - minx) * (maxy - miny)
return area
def minAreaRect(self, points):
S = set(map(tuple, points))
ans = float("inf")
for j, p2 in enumerate(points):
for i in range(j):
p1 = points[i]
if (
p1[0] != p2[0]
and p1[1] != p2[1]
and (p1[0], p2[1]) in S
and (p2[0], p1[1]) in S
):
ans = min(ans, abs(p2[0] - p1[0]) * abs(p2[1] - p1[1]))
return ans if ans < float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
x_axes = {}
y_axes = {}
for point in points:
if point[0] not in x_axes:
x_axes[point[0]] = set()
if point[1] not in y_axes:
y_axes[point[1]] = set()
x_axes[point[0]].add(point[1])
y_axes[point[1]].add(point[0])
def findMinArea(point, x_axes, y_axes):
possible_x = y_axes[point[1]]
possible_y = x_axes[point[0]]
min_area = None
for x in possible_x:
if x == point[0]:
continue
for y in possible_y:
if y == point[1]:
continue
if x in y_axes[y]:
area = abs((x - point[0]) * (y - point[1]))
min_area = area if min_area is None else min(area, min_area)
return min_area
min_area = None
for point in points:
area = findMinArea(point, x_axes, y_axes)
if area is not None:
min_area = area if min_area is None else min(min_area, area)
return min_area if min_area else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NONE FOR VAR VAR IF VAR VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NONE VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR NONE FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE ASSIGN VAR VAR NONE VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
length = len(points)
if length <= 3:
return 0
sets = set([])
for point in points:
sets.add(tuple(point))
min_area = float("inf")
for i in range(length - 1):
x1, y1 = points[i]
for j in range(i + 1, length):
x2, y2 = points[j]
if x1 == x2 or y1 == y2:
continue
if (x1, y2) in sets and (x2, y1) in sets:
area = abs(x1 - x2) * abs(y1 - y2)
if area < min_area:
min_area = area
if min_area == float("inf"):
return 0
return min_area | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
counter = Counter(list(map(tuple, points)))
if any(count >= 4 for count in list(counter.values())):
return 0
points = list(counter)
result = float("inf")
for i, (x, y) in enumerate(points):
for j in range(i + 1, len(points)):
x_, y_ = points[j]
if x != x_ and y != y_:
if (x, y_) in counter and (x_, y) in counter:
result = min(result, abs((x_ - x) * (y_ - y)))
if not result:
break
return 0 if result == float("inf") else result | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
n = len(points)
nx = len(set(x for x, y in points))
ny = len(set(y for x, y in points))
if nx == n or ny == n:
return 0
lastx = {}
everyx = collections.defaultdict(list)
min_area = math.inf
for x, y in points:
everyx[x].append(y)
for x in sorted(everyx):
everyy = sorted(everyx[x])
for i, y1 in enumerate(everyy):
for j in range(i):
y2 = everyy[j]
if (y1, y2) in lastx:
area = abs(y2 - y1) * abs(x - lastx[y1, y2])
min_area = min(area, min_area)
lastx[y1, y2] = x
return min_area if min_area < math.inf else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
coord = {(x, y) for x, y in points}
n = len(points)
min_area = sys.maxsize
for i in range(n):
for j in range(i + 1, n):
x1, y1 = points[i]
x2, y2 = points[j]
if x1 == x2 or y1 == y2:
continue
if (x1, y2) in coord and (x2, y1) in coord:
min_area = min(min_area, abs((x1 - x2) * (y1 - y2)))
return min_area if min_area != sys.maxsize else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
vsides = dict()
for x, y in points:
if x in vsides:
vsides[x].append(y)
else:
vsides[x] = [y]
slengths = dict()
min_area = float("inf")
xs = list(vsides.keys())
xs.sort()
for x in xs:
vs = vsides[x]
vs.sort()
for i in range(len(vs) - 1):
for j in range(i + 1, len(vs)):
if (vs[i], vs[j]) in slengths:
area = abs((x - slengths[vs[i], vs[j]]) * (vs[j] - vs[i]))
min_area = min(min_area, area)
slengths[vs[i], vs[j]] = x
return min_area if min_area != float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
if len(points) < 4:
return 0
x_coord = collections.defaultdict(list)
points.sort(key=lambda x: (x[0], x[1]))
min_area = float("inf")
for i in range(len(points)):
for j in range(i + 1, len(points)):
x1, y1 = points[i]
x2, y2 = points[j]
if x1 == x2:
if (y1, y2) in x_coord:
x_other = x_coord[y1, y2][-1]
min_area = min(min_area, (y2 - y1) * (x1 - x_other))
x_coord[y1, y2].append(x1)
return min_area if min_area != float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
y2x = collections.defaultdict(list)
for x, y in points:
y2x[y].append(x)
res = float("inf")
ys = list(y2x.keys())
for y1 in ys:
for y2 in ys:
if y1 != y2:
x1List = y2x[y1]
x2Set = y2x[y2]
canList = []
for x1 in x1List:
if x1 in x2Set:
canList.append(x1)
for m in canList:
for n in canList:
if m != n:
res = min(res, abs(m - n) * abs(y1 - y2))
if res == float("inf"):
return 0
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
point_set = set((x, y) for x, y in points)
min_rec = math.inf
for x1, y1 in points:
for x2, y2 in points:
if x1 < x2 and y1 > y2:
if (x1, y2) in point_set and (x2, y1) in point_set:
min_rec = min(min_rec, (x2 - x1) * (y1 - y2))
return 0 if min_rec == math.inf else min_rec | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR VAR NUMBER VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
res = inf = float("inf")
seen = set()
n = len(points)
for x1, y1 in points:
for x2, y2 in seen:
if (x1, y2) in seen and (x2, y1) in seen:
res = min(res, abs((x2 - x1) * (y2 - y1)))
seen.add((x1, y1))
return res if res != inf else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
columns = defaultdict(list)
lastx = {}
ans = sys.maxsize
for x, y in points:
columns[x].append(y)
for x in sorted(columns):
y_list = columns[x]
y_list.sort()
for i, y1 in enumerate(y_list):
for j in range(i):
y2 = y_list[j]
if (y1, y2) in lastx:
ans = min(ans, (x - lastx[y1, y2]) * (y1 - y2))
lastx[y1, y2] = x
return ans if ans < sys.maxsize else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
point_dict = {}
point_list = []
for i in points:
point_dict[i[0], i[1]] = 1
point_list.append((i[0], i[1]))
print(point_dict)
print(point_list)
ans = math.inf
for i in range(0, len(points)):
for j in range(i, len(points)):
ix, iy = point_list[i]
jx, jy = point_list[j]
if ix == jx or iy == jy:
continue
if (ix, jy) in point_dict and (jx, iy) in point_dict:
ans = min(ans, abs(ix - jx) * abs(iy - jy))
if ans == math.inf:
return 0
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
s = set(map(tuple, points))
dx = collections.defaultdict(list)
dy = collections.defaultdict(list)
for x, y in points:
dx[x].append(y)
dy[y].append(x)
res = float("inf")
for x in sorted(dx.keys()):
for i in range(len(dx[x])):
y1 = dx[x][i]
for j in range(i + 1, len(dx[x])):
y2 = dx[x][j]
for x1 in dy[y2]:
if x1 <= x:
continue
if (x1, y1) in s:
res = min(res, abs(x - x1) * abs(y1 - y2))
return res if res != float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
lines = self.find_lines(points)
min_area = 0
d = {}
for x, ys in lines:
if ys in d:
px = d[ys]
curr_min_area = abs(x - px) * abs(ys[0] - ys[1])
min_area = (
min(min_area, curr_min_area) if min_area > 0 else curr_min_area
)
d[ys] = x
return min_area
def find_lines(self, points: List[List[int]]) -> List[Tuple[int, Tuple[int, int]]]:
ps = defaultdict(list)
for x, y in sorted(points):
ps[x].append(y)
lines = []
for x, ys in ps.items():
for i, y0 in enumerate(ys):
for y1 in ys[i + 1 :]:
lines.append((x, (y0, y1)))
return lines | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR VAR VAR VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
def hashing(i, j, p=40001):
return i * p + j
board = set()
for x, y in points:
board.add(hashing(x, y))
ans = float("inf")
print(board)
for i in range(len(points)):
for j in range(i + 1, len(points)):
tlx, tly = points[i]
brx, bry = points[j]
if tlx == brx or tly == bry:
continue
if hashing(tlx, bry) in board and hashing(brx, tly) in board:
ans = min(ans, abs((brx - tlx) * (bry - tly)))
if ans < float("inf"):
return ans
else:
return 0 | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR STRING RETURN VAR RETURN NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
if not points:
return 0
x_map = {}
points.sort(key=lambda x: x)
area = float("inf")
y_map = {}
for point in points:
if point[0] in x_map:
x_map[point[0]].append(point[1])
continue
x_map[point[0]] = [point[1]]
for x in x_map:
x_map[x].sort()
for j in range(0, len(x_map[x])):
y2 = x_map[x][j]
for k in range(0, j):
y1 = x_map[x][k]
if (y1, y2) in y_map:
area = min(area, (y2 - y1) * (x - y_map[y1, y2]))
y_map[y1, y2] = x
return area if area != float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER LIST VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
rows = collections.defaultdict(list)
points.sort()
seen = set()
result = float("inf")
for x1, y1 in points:
for x2, y2 in seen:
if x1 < x2 and y1 < y2:
continue
if (x1, y2) in seen and (x2, y1) in seen:
area = abs(x1 - x2) * abs(y1 - y2)
if area and area < result:
result = area
seen.add((x1, y1))
return result if result < float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
dx = defaultdict(list)
for x, y in points:
dx[x].append(y)
lastx = defaultdict(list)
ans = float("inf")
for x in sorted(dx):
col = dx[x]
col.sort()
for i in range(len(col) - 1):
for j in range(i + 1, len(col)):
y1 = col[i]
y2 = col[j]
if (y1, y2) in lastx:
for xs in lastx[y1, y2]:
ans = min(ans, abs(y2 - y1) * abs(x - xs))
lastx[y1, y2].append(x)
return ans if ans < float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
n = len(points)
nx = len(set(x for x, y in points))
ny = len(set(y for x, y in points))
if nx == n or ny == n:
return 0
res = 2**31
s = {(p[0], p[1]) for p in points}
for i, p1 in enumerate(points):
for j, p2 in enumerate(points[i + 1 :]):
if p1[0] != p2[0] and p1[1] != p2[1]:
if (p1[0], p2[1]) in s and (p2[0], p1[1]) in s:
res = min(res, abs(p1[1] - p2[1]) * abs(p1[0] - p2[0]))
res = res if res < 2**31 else 0
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER NUMBER VAR NUMBER RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
if len(points) < 4:
return 0
seen = set()
minarea = sys.maxsize
for x1, y1 in points:
for x2, y2 in seen:
if (x1, y2) in seen and (x2, y1) in seen:
minarea = min(minarea, abs(x2 - x1) * abs(y2 - y1))
seen.add((x1, y1))
return minarea if minarea < sys.maxsize else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
xtoy = collections.defaultdict(set)
for x, y in points:
xtoy[x].add(y)
minarea = sys.maxsize
for x1 in xtoy:
for x2 in xtoy:
if x1 >= x2:
continue
commony = list(xtoy[x1].intersection(xtoy[x2]))
if len(commony) < 2:
continue
commony.sort()
width = x2 - x1
height = commony[1] - commony[0]
for i in range(2, len(commony)):
height = min(height, commony[i] - commony[i - 1])
minarea = min(minarea, height * width)
return minarea if minarea != sys.maxsize else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
points = sorted(points)
points_set = set([tuple(p) for p in points])
min_area = 999999999999
has_rectangle = False
for i in range(len(points)):
for j in range(i + 1, len(points)):
x1 = points[i][0]
y1 = points[i][1]
x2 = points[j][0]
y2 = points[j][1]
if x1 == x2:
continue
if y1 == y2:
continue
if (x1, y2) not in points_set:
continue
if (x2, y1) not in points_set:
continue
area = abs((x1 - x2) * (y1 - y2))
has_rectangle = True
if area < min_area:
min_area = area
if not has_rectangle:
return 0
else:
return min_area | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR RETURN NUMBER RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
pointsSet = set([(x, y) for x, y in points])
minv = float("inf")
for i in range(len(points)):
for j in range(i + 1, len(points)):
if (
points[i][0] != points[j][0]
and points[i][1] != points[j][1]
and (points[i][0], points[j][1]) in pointsSet
and (points[j][0], points[i][1]) in pointsSet
):
minv = min(
minv,
abs(points[i][0] - points[j][0])
* abs(points[i][1] - points[j][1]),
)
if minv == 1:
print((points[i], points[j]))
return minv if minv != float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
point_set = set([tuple(p) for p in points])
min_area = float("inf")
for idx, p1 in enumerate(points):
for idx2 in range(idx, len(points)):
p2 = points[idx2]
if p1[0] != p2[0] and p1[1] != p2[1]:
p3 = p1[0], p2[1]
p4 = p2[0], p1[1]
if p3 in point_set and p4 in point_set:
dx = abs(p1[0] - p2[0])
dy = abs(p1[1] - p2[1])
area = dx * dy
if area < min_area:
min_area = area
return min_area if min_area != float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
pointset = {tuple(i) for i in points}
ans = float("inf")
area = lambda x, y: abs((x[0] - y[0]) * (x[1] - y[1]))
for i, A in enumerate(points):
for j in range(i):
B = points[j]
if A[0] == B[0] or A[1] == B[1]:
continue
if (A[0], B[1]) in pointset and (B[0], A[1]) in pointset:
ans = min(ans, area(A, B))
return ans if ans != float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
table = {}
mark = []
best = 0
for i in points:
if i[0] not in table:
table[i[0]] = [[i[1]], []]
elif i[1] not in table[i[0]][0]:
for j in table[i[0]][0]:
if j < i[1]:
table[i[0]][1].append((j, i[1]))
else:
table[i[0]][1].append((i[1], j))
table[i[0]][0].append(i[1])
if len(table[i[0]][0]) == 2:
mark.append(i[0])
g = {}
if len(mark) < 2:
return 0
for i in mark:
p = table[i]
for j in p[1]:
if j in g:
for k in g[j]:
temp = abs((k - i) * (j[1] - j[0]))
if best == 0 or temp < best:
best = temp
g[j].append(i)
else:
g[j] = [i]
return best | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER LIST LIST VAR NUMBER LIST IF VAR NUMBER VAR VAR NUMBER NUMBER FOR VAR VAR VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR NUMBER IF VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
n = len(points)
nx, ny = len(set(x for x, _ in points)), len(set(y for _, y in points))
if nx == n or ny == n:
return 0
p = collections.defaultdict(list)
if nx > ny:
for x, y in points:
p[x].append(y)
else:
for x, y in points:
p[y].append(x)
res = float("inf")
dic_last = {}
for x1, x2 in itertools.combinations(p, 2):
if len(p[x1]) < 2 or len(p[x2]) < 2:
continue
l1, l2 = sorted(p[x1]), sorted(p[x2])
i = j = 0
y = []
while i < len(l1) and j < len(l2):
v = max(l1[i], l2[j])
if l1[i] < v:
i += 1
elif l2[j] < v:
j += 1
else:
y += (v,)
i, j = i + 1, j + 1
for y1, y2 in itertools.combinations(y, 2):
res = min(res, abs(x1 - x2) * (y2 - y1))
return res if res < float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
seen = set()
best = 0
for p1 in points:
x1, y1 = p1
for p2 in seen:
x2, y2 = p2
if (x1, y2) in seen and (x2, y1) in seen:
area = abs(x2 - x1) * abs(y2 - y1)
if best == 0:
best = area
else:
best = min(best, area)
seen.add((x1, y1))
return best | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | import itertools
class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
if not points:
return 0
ybucket = defaultdict(list)
for x, y in points:
ybucket[y].append(x)
for k, v in ybucket.items():
if len(v) >= 2:
ybucket[k] = sorted(v)
ys = [k for k in ybucket.keys() if len(ybucket[k]) >= 2]
ans = 2**31 - 1
for y1, y2 in itertools.combinations(ys, 2):
row1 = set(ybucket[y1])
row2 = ybucket[y2]
for idx, x in enumerate(row2):
if x in row1:
print(x)
xlast = x
idx_last = idx
break
else:
continue
dy = abs(y2 - y1)
for x in row2[idx_last + 1 :]:
if x in row1:
ans = min(ans, abs(x - xlast) * dy)
xlast = x
return ans if ans < 2**31 - 1 else 0 | IMPORT CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
if len(points) < 4:
return 0
x_vals = defaultdict(set)
y_vals = defaultdict(set)
for p in points:
x_vals[p[0]].add(p[1])
y_vals[p[1]].add(p[0])
min_area = 40000 * 40000 + 1
for x, y_coords in list(x_vals.items()):
if len(y_coords) == 1:
continue
y_list = list(y_coords)
for i, y_1 in enumerate(y_list):
for j, y_2 in enumerate(y_list[i + 1 :]):
common_x_2 = y_vals[y_1].intersection(y_vals[y_2])
common_x_2.remove(x)
for x_2 in list(common_x_2):
min_area = min(min_area, abs(x_2 - x) * abs(y_2 - y_1))
if min_area == 40000 * 40000 + 1:
return 0
return min_area | CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN NUMBER RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points):
pSet, ans = set(map(tuple, points)), float("inf")
for i, (p1x, p1y) in enumerate(points):
for p2x, p2y in points[i + 1 :]:
if (
p1x != p2x
and p1y != p2y
and (p1x, p2y) in pSet
and (p2x, p1y) in pSet
):
ans = min(ans, abs((p1x - p2x) * (p1y - p2y)))
return ans if ans < float("inf") else 0
class Solution:
def minAreaRect(self, points):
X = list(zip(*points))
if len(set(X[0])) == 1 or len(set(X[1])) == 1:
return 0
pSet, ans = set(map(tuple, points)), float("inf")
for i, (p1x, p1y) in enumerate(points):
for p2x, p2y in points[i + 1 :]:
area = abs((p2x - p1x) * (p2y - p1y))
if area > ans or area == 0:
continue
if (p1x, p2y) in pSet and (p2x, p1y) in pSet:
ans = area
return ans if ans < float("inf") else 0 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
if not points:
return 0
ret = float("inf")
r_to_c = collections.defaultdict(list)
for r, c in points:
r_to_c[r].append(c)
seen = {}
for bottom in sorted(r_to_c.keys()):
cols = r_to_c[bottom]
cols.sort()
for right_i in range(len(cols)):
for left_i in range(right_i):
left = cols[left_i]
right = cols[right_i]
if (left, right) in seen:
top = seen[left, right]
ret = min(ret, (bottom - top) * (right - left))
seen[left, right] = bottom
if ret == float("inf"):
return 0
return ret | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
res = sys.maxsize
diag = collections.defaultdict(set)
for i, j in points:
diag[i].add(j)
for i in range(len(points)):
a1, a2 = points[i]
for j in range(len(points)):
b1, b2 = points[j]
if not (b1 > a1 and b2 > a2):
continue
if b2 in diag[a1] and a2 in diag[b1]:
res = min(res, abs(a1 - b1) * abs(a2 - b2))
return res if res != sys.maxsize else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR VAR VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
def smallestinterval(l):
rst = float("inf")
for i in range(1, len(l)):
if l[i] - l[i - 1] < rst:
rst = l[i] - l[i - 1]
return rst
xloc = defaultdict(set)
for p in points:
xloc[p[0]].add(p[1])
minarea = float("inf")
xkeys = list(xloc.keys())
for x1 in xkeys:
for x2 in xkeys:
if x1 != x2:
sharedy = xloc[x1] & xloc[x2]
if len(sharedy) > 1:
ally = list(sharedy)
ally.sort()
smallinterval = smallestinterval(ally)
if smallinterval * abs(x1 - x2) < minarea:
minarea = smallinterval * abs(x1 - x2)
if minarea == float("inf"):
return 0
return minarea | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | MAX_VAL = 10**9 + 7
class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
points_set = set()
for x, y in points:
points_set.add((x, y))
n = len(points)
min_area = MAX_VAL
for i in range(n - 1):
for j in range(i + 1, n):
x1, y1 = points[i]
x2, y2 = points[j]
if (
x1 != x2
and y1 != y2
and (x2, y1) in points_set
and (x1, y2) in points_set
):
curr_area = abs((x1 - x2) * (y1 - y2))
min_area = min(min_area, curr_area)
return 0 if min_area == MAX_VAL else min_area | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR NUMBER VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
points_set = set([tuple(v) for v in points])
min_area = None
for i, x1y1 in enumerate(points):
for x2y2 in points[i:]:
if x1y1[0] == x2y2[0] or x1y1[1] == x2y2[1]:
continue
x1y2 = tuple([x1y1[0], x2y2[1]])
x2y1 = tuple([x2y2[0], x1y1[1]])
if x1y2 in points_set and x2y1 in points_set:
area = abs((x1y1[0] - x2y2[0]) * (x1y1[1] - x2y2[1]))
if min_area is None or area < min_area:
min_area = area
return min_area if min_area else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NONE VAR VAR ASSIGN VAR VAR RETURN VAR VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | import sys
class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
minArea = sys.maxsize
pointSet = set()
for point in points:
pointSet.add((point[0], point[1]))
for i in range(0, len(points)):
for j in range(i + 1, len(points)):
point1 = points[i]
point2 = points[j]
x1, y1 = point1[0], point1[1]
x2, y2 = point2[0], point2[1]
if x1 == x2 or y1 == y2:
continue
if (x1, y2) in pointSet and (x2, y1) in pointSet:
area = abs(x2 - x1) * abs(y2 - y1)
minArea = min(minArea, area)
if minArea == sys.maxsize:
return 0
return minArea | IMPORT CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
point_set = set([tuple(x) for x in points])
min_size = float("inf")
n = len(points)
for i in range(n):
x1, y1 = points[i]
for j in range(i + 1, n):
x2, y2 = points[j]
if x1 == x2 or y1 == y2:
continue
if (x1, y2) in point_set and (x2, y1) in point_set:
size = (y2 - y1) * (x2 - x1)
min_size = min(min_size, abs(size))
continue
if min_size == float("inf"):
return 0
return min_size | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
x_to_points = defaultdict(lambda: [])
y_to_points = defaultdict(lambda: set())
for [x, y] in points:
x_to_points[x].append((x, y))
y_to_points[y].add((x, y))
min_rect = float("inf")
for x in sorted(x_to_points.keys()):
xpoints = x_to_points[x]
if len(xpoints) < 2:
continue
for x1_index in range(len(xpoints)):
for x2_index in range(x1_index + 1, len(xpoints)):
x1, y1 = xpoints[x1_index]
x2, y2 = xpoints[x2_index]
for x3, y3 in y_to_points[y1]:
if x3 == x2:
continue
if (x3, y2) in y_to_points[y2]:
min_rect = min(min_rect, abs(y2 - y1) * abs(x3 - x2))
return 0 if min_rect == float("inf") else min_rect | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
xd = collections.defaultdict(set)
for p in points:
xd[p[0]].add(p[1])
yd = collections.defaultdict(list)
for x, v in list(xd.items()):
if len(v) < 2:
continue
ys = list(v)
for i in range(len(ys)):
for j in range(i + 1, len(ys)):
miny, maxy = min(ys[i], ys[j]), max(ys[i], ys[j])
yd[miny, maxy].append(x)
res = float("inf")
for ypair, xlist in list(yd.items()):
if len(xlist) < 2:
continue
xlist = sorted(xlist)
h = ypair[1] - ypair[0]
for i in range(len(xlist) - 1):
res = min(res, h * (xlist[i + 1] - xlist[i]))
return res if res != float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution(object):
def minAreaRect(self, points):
y_group = collections.defaultdict(list)
for x, y in points:
y_group[x].append(y)
visited = {}
area = inf
for x in sorted(y_group):
Y = y_group[x]
Y.sort()
for i in range(len(Y)):
y1 = Y[i]
for j in range(i):
y2 = Y[j]
if (y1, y2) in visited:
area = min((x - visited[y1, y2]) * (y1 - y2), area)
visited[y1, y2] = x
return area if area < inf else 0 | CLASS_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR NUMBER |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
columns = defaultdict(list)
points = sorted(points, key=lambda x: (x[0], x[1]))
for x, y in points:
columns[x].append(y)
most_recent = {}
min_area = float("inf")
for x in columns:
column = columns[x]
for index, y2 in enumerate(column):
for y1 in column[:index]:
if (y1, y2) in most_recent:
min_area = min(min_area, (y2 - y1) * (x - most_recent[y1, y2]))
most_recent[y1, y2] = x
return min_area if min_area < float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
points_dict = collections.defaultdict(list)
for x, y in points:
points_dict[x].append(y)
points_dict = dict(sorted(list(points_dict.items()), key=lambda x: x[0]))
seen_ys = dict()
result = float("inf")
for x in list(points_dict.keys()):
ys = sorted(points_dict[x])
print((x, ys))
for i, y2 in enumerate(ys):
for j in range(i):
y1 = ys[j]
if (y1, y2) in seen_ys:
result = min(result, (y2 - y1) * (x - seen_ys[y1, y2]))
seen_ys[y1, y2] = x
return result if result < float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
d = collections.defaultdict(set)
for i in range(len(points)):
x0, y0 = points[i]
for j in range(i + 1, len(points)):
x1, y1 = points[j]
if x0 == x1:
d[x0].add(tuple(sorted([y0, y1])))
seen = set()
minA = float("inf")
for x0, lines0 in d.items():
seen.add(x0)
for x1, lines1 in d.items():
if x1 not in seen:
for l in lines0:
if l in lines1:
minA = min(minA, abs(x0 - x1) * (l[1] - l[0]))
return minA if minA < float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR IF VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, z: List[List[int]]) -> int:
x = defaultdict(set)
y = defaultdict(set)
z.sort()
for a, b in z:
x[a].add(b)
y[b].add(a)
ans = inf
l = len(z)
for k in range(l):
a, b = z[k]
for m in range(k + 1, l):
c, d = z[m]
if a < c:
break
for j in y[b].intersection(y[d]):
if j != a:
ans = min(ans, abs(b - d) * abs(a - j))
return ans if ans != inf else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR VAR VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
points.sort()
min_area = 40000**2 + 1
m = len(points)
points_table = set()
for x, y in points:
points_table.add((x, y))
for i in range(m):
for j in range(i + 1, m):
a = points[i]
b = points[j]
if a[0] != b[0] and a[1] != b[1]:
if (a[0], b[1]) in points_table and (b[0], a[1]) in points_table:
z = abs((b[0] - a[0]) * (b[1] - a[1]))
min_area = min(min_area, z)
if min_area == 40000**2 + 1:
min_area = 0
return min_area | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
res = float("inf")
dic = collections.defaultdict(set)
for p in points:
dic[p[0]].add(p[1])
for i in range(len(points)):
for j in range(len(points)):
p1, p2 = points[i], points[j]
if p1[0] >= p2[0] or p1[1] >= p2[1]:
continue
if p1[1] in dic[p2[0]] and p2[1] in dic[p1[0]]:
res = min(res, (p2[0] - p1[0]) * (p2[1] - p1[1]))
return res if res != float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
n = len(points)
if n < 4:
return 0
s = set([tuple(i) for i in points])
d = {}
for i in points:
if i[0] in d:
d[i[0]].append(i[1])
else:
d[i[0]] = [i[1]]
for i in list(d.keys()):
if len(d[i]) == 1:
d.pop(i)
dkeys = list(d.keys())
minarea = float("inf")
for i in range(len(dkeys) - 1):
if len(points[i]) == 1:
continue
for x in range(len(d[dkeys[i]]) - 1):
for y in range(x + 1, len(d[dkeys[i]])):
for j in range(i + 1, len(dkeys)):
if (dkeys[j], d[dkeys[i]][x]) in s and (
dkeys[j],
d[dkeys[i]][y],
) in s:
minarea = min(
minarea,
abs(dkeys[i] - dkeys[j])
* abs(d[dkeys[i]][x] - d[dkeys[i]][y]),
)
if minarea == float("inf"):
return 0
return minarea | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER LIST VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
ys = {}
for x, y in points:
if y not in ys:
ys[y] = set()
ys[y].add(x)
result = -1
for y1 in ys:
for y2 in ys:
if y1 == y2:
continue
points = ys[y1].intersection(ys[y2])
if len(points) < 2:
continue
points = list(points)
points.sort()
x1 = points[0]
for i in range(1, len(points)):
x2 = points[i]
area = (x2 - x1) * abs(y2 - y1)
if result < 0:
result = area
else:
result = min(area, result)
x1 = x2
return result if result >= 0 else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN VAR NUMBER VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
coords = {}
for x, y in points:
if x not in coords:
coords[x] = {}
coords[x][y] = True
area = None
for x1 in coords:
for y1 in coords[x1]:
for y2 in coords[x1]:
if y1 == y2:
continue
for x2 in coords:
if x1 == x2:
continue
if y1 in coords[x2] and y2 in coords[x2]:
current = abs(x1 - x2) * abs(y1 - y2)
if area == None or current < area:
area = current
if area == None:
return 0
return area | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR DICT ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NONE FOR VAR VAR FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR FOR VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR IF VAR NONE RETURN NUMBER RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution(object):
def minAreaRect(self, points: List[List[int]]) -> int:
n = len(points)
setx = set()
sety = set()
for x, y in points:
setx.add(x)
sety.add(y)
nx = len(setx)
ny = len(sety)
if nx == n or ny == n:
return 0
p = collections.defaultdict(list)
if nx > ny:
for x, y in points:
p[x].append(y)
else:
for x, y in points:
p[y].append(x)
res = float("inf")
dic_last = {}
for x in sorted(p):
p[x].sort()
for y1, y2 in itertools.combinations(p[x], 2):
if (y1, y2) in dic_last:
res = min(res, (x - dic_last[y1, y2]) * (y2 - y1))
dic_last[y1, y2] = x
return res if res < float("inf") else 0 | CLASS_DEF VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
pointrec = dict()
for x, y in points:
if not x in pointrec:
pointrec[x] = dict()
pointrec[x][y] = 1
area = 16 * 10**8
for i in range(len(points) - 1):
for j in range(i + 1, len(points)):
a = abs((points[i][0] - points[j][0]) * (points[i][1] - points[j][1]))
if a == 0:
continue
if (
a < area
and points[j][1] in pointrec[points[i][0]]
and points[i][1] in pointrec[points[j][0]]
):
area = a
if area == 16 * 10**8:
area = 0
return area | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
def compute_table(points, axis=0):
table = {}
for point in points:
key, value = point[axis], point[1 - axis]
if key in table:
table[key].append(value)
else:
table[key] = [value]
return table
points.sort()
x_2_y = compute_table(points, 0)
y_2_x = compute_table(points, 1)
min_area = float("inf")
hashed = set([tuple(point) for point in points])
for point in points:
x, y = point
y_candidates = [_y for _y in x_2_y[x] if _y > y]
x_candidates = [_x for _x in y_2_x[y] if _x > x]
for c_y in y_candidates:
for c_x in x_candidates:
if (c_x, c_y) in hashed:
min_area = min(min_area, abs(c_y - y) * abs(c_x - x))
if min_area == float("inf"):
return 0
return min_area | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR FOR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
points.sort()
xs = collections.defaultdict(list)
ys = collections.defaultdict(list)
for x, y in points:
xs[x].append(y)
ys[y].append(x)
result = math.inf
for x0, ylist in xs.items():
for y1, y2 in itertools.combinations(ylist, 2):
xlist1, xlist2 = ys[y1], ys[y2]
m, n = len(xlist1), len(xlist2)
i = bisect.bisect(xlist1, x0)
if i == m:
continue
j = bisect.bisect(xlist2, x0)
if j == n:
continue
while i < m and j < n:
if xlist1[i] < xlist2[j]:
i += 1
elif xlist1[i] > xlist2[j]:
j += 1
else:
result = min(result, (xlist1[i] - x0) * (y2 - y1))
break
return 0 if math.isinf(result) else result | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
ans = float("inf")
seen = set(map(tuple, points))
points.sort()
for i in range(len(points)):
for j in range(i):
x1, y1 = points[i][0], points[i][1]
x2, y2 = points[j][0], points[j][1]
if x1 != x2 and y1 != y2 and (x1, y2) in seen and (x2, y1) in seen:
ans = min(ans, (x1 - x2) * abs(y2 - y1))
return ans if ans != float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
pointset = {tuple(i) for i in points}
ans = float("inf")
area = lambda x, y: abs((x[0] - y[0]) * (x[1] - y[1]))
for A, B in combinations(points, 2):
if A[0] == B[0] or A[1] == B[1]:
continue
if (A[1] - B[1]) // (A[0] - B[0]) >= 0:
continue
C = A[0], B[1]
D = B[0], A[1]
if C in pointset and D in pointset:
ans = min(ans, area(A, B))
return ans if ans != float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | from itertools import combinations
class Solution:
def minAreaRect(self, points) -> int:
point_x = {}
point_y = {}
result = float("inf")
for i in points:
if point_x.get(i[0]):
point_x[i[0]].add(i[1])
else:
point_x[i[0]] = set([i[1]])
if point_y.get(i[1]):
point_y[i[1]].add(i[0])
else:
point_y[i[1]] = set([i[0]])
for i in point_x:
if len(point_x[i]) < 2:
continue
for y1, y2 in combinations(point_x[i], 2):
inter = sorted(point_y[y1].intersection(point_y[y2]))
if len(inter) >= 2:
result = min(
min([(inter[j] - inter[j - 1]) for j in range(1, len(inter))])
* abs(y1 - y2),
result,
)
if result == float("inf"):
result = 0
return result | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR LIST VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR LIST VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER RETURN VAR VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
pointsSet = set(tuple(point) for point in points)
minarea = sys.maxsize
for i in range(len(points)):
for j in range(i + 1, len(points)):
x_i = points[i][0]
y_i = points[i][1]
x_j = points[j][0]
y_j = points[j][1]
if (
x_i != x_j
and y_i != y_j
and (x_i, y_j) in pointsSet
and (x_j, y_i) in pointsSet
):
minarea = min(minarea, abs(x_j - x_i) * abs(y_j - y_i))
return minarea if minarea != sys.maxsize else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR VAR VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | import sys
class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
S = set([(x, y) for x, y in points])
mini = sys.maxsize
for i, p1 in enumerate(points):
for j in range(i):
p2 = points[j]
if (
p1[0] != p2[0]
and p1[1] != p2[1]
and (p1[0], p2[1]) in S
and (p2[0], p1[1]) in S
):
mini = min(mini, abs((p1[1] - p2[1]) * (p1[0] - p2[0])))
return mini if mini != sys.maxsize else 0 | IMPORT CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR VAR NUMBER VAR |
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
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
point_set = set(map(tuple, points))
min_area = float("inf")
for i, p1 in enumerate(points):
for j in range(i):
p2 = points[j]
potential_area = abs(p1[0] - p2[0]) * abs(p1[1] - p2[1])
if potential_area == 0 or potential_area > min_area:
continue
if (p1[0], p2[1]) in point_set and (p2[0], p1[1]) in point_set:
min_area = min(potential_area, min_area)
return 0 if min_area == float("inf") else min_area | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.