坐标轴中的矩形

题目链接: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]);
        }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值