题目链接:https://leetcode-cn.com/problems/minimum-area-rectangle
1.搜索(能构成矩形,查找条件是复合的)目标(四个点)类题目
代码的执行是顺序的,满足条件一(x轴坐标相同)的元素下->满足条件二(Y轴坐标相同)
将所有X轴相同的点放在一起,按照x从小到大的顺序遍历。
判断以x中两个点p1,p2作为right边的最小矩形,单调栈以及DP的思想。
2.程序中描述一个坐标系中的 矩形:上边Y轴坐标(top),下边Y轴坐标(bottom),左边X轴坐标(left),右边X轴坐标(right)。
3.代码借鉴:
Map<Integer, List<Integer>> rows = new TreeMap();//将key值排序好了
for (int[] point : points) {
int x = point[0], y = point[1];
rows.computeIfAbsent(x, z -> new ArrayList()).add(y);// 每个X轴上所有的Y轴坐标值
}
int ans = Integer.MAX_VALUE;
Map<Integer, Integer> lastx = new HashMap();
for (int x : rows.keySet()) {
List<Integer> row = rows.get(x);
Collections.sort(row);
for (int i = 0; i < row.size(); ++i)
for (int j = i + 1; j < row.size(); ++j) {
int y1 = row.get(i), y2 = row.get(j);
int code = 40001 * y1 + y2;//数字对拼接成字符串判断存在
if (lastx.containsKey(code))
ans = Math.min(ans, (x - lastx.get(code)) * (y2 - y1));
lastx.put(code, x);
}
}
return ans < Integer.MAX_VALUE ? ans : 0;
题目链接:https://leetcode-cn.com/problems/maximum-product-of-three-numbers/
1.搜索(三个)目标(数字)类题目
预处理,集合代表元素(最值问题优先看代表元素,最大最小
3.
public int maximumProduct(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
//包含了多种情况,正数0个,1个,2个,3个
return Math.max(nums[0] * nums[1] * nums[n - 1], nums[n - 3] * nums[n - 2] * nums[n - 1]);
}