当前位置: 首页 > news >正文

Leetcode 3047. Find the Largest Area of Square Inside Two Rectangles

  • Leetcode 3047. Find the Largest Area of Square Inside Two Rectangles
    • 1. 解题思路
    • 2. 代码实现
  • 题目链接:3047. Find the Largest Area of Square Inside Two Rectangles

1. 解题思路

这道题倒是没啥特别的思路,直接暴力求解就是了,因此就是一个 O ( N 2 ) O(N^2) O(N2)的二重循环遍历任意两个矩形交叉部分当中可能的最大的正方形的面积。

然后,关于交叉部分的最大正方形的面积,这里又是一个分类讨论的问题,我们需要讨论以下两个矩形的分布情况,然后给出最终的答案。

2. 代码实现

给出python代码实现如下:

class Solution:def largestSquareArea(self, bottomLeft: List[List[int]], topRight: List[List[int]]) -> int:ans = 0n = len(bottomLeft)def get_intersection(rec1, rec2):if rec1[0] > rec2[0]:return get_intersection(rec2, rec1)if rec1[2] <= rec2[0]:return 0elif rec1[1] >= rec2[3] or rec1[3] <= rec2[1]:return 0l = min(rec2[2], rec1[2]) - rec2[0]if rec1[1] <= rec2[1]:h = min(rec1[3], rec2[3]) - rec2[1]else:h = min(rec1[3], rec2[3]) - rec1[1]d = min(h, l)return d * dfor i in range(n):rec1 = [bottomLeft[i][0], bottomLeft[i][1], topRight[i][0], topRight[i][1]]for j in range(i+1, n):rec2 = [bottomLeft[j][0], bottomLeft[j][1], topRight[j][0], topRight[j][1]]ans = max(ans, get_intersection(rec1, rec2))return ans

提交代码评测得到:耗时4690ms,占用内存17.4MB。

http://www.lryc.cn/news/306285.html

相关文章:

  • ELK 简介安装
  • Linux 的交换空间(swap)是什么?有什么用?
  • 消息中间件篇之RabbitMQ-消息不丢失
  • MongoDB中的TTL索引:自动过期数据的深入解析与使用方式
  • IPV6地址
  • 解密API关键词搜索(淘宝京东1688)商品列表数据
  • wpf 简单实验 数据更新 列表更新
  • 【Flink精讲】Flink性能调优:内存调优
  • Java 中常用的数据结构类 API
  • JavaScript学习小记(1)基本数据结构(数组,字符串)
  • python opencv实现车牌识别
  • K8S节点GPU虚拟化(vGPU)
  • NLP 使用Word2vec实现文本分类
  • 【Redis学习笔记03】Java客户端
  • 神经网络系列---激活函数
  • python中continue的对比理解
  • Amazon Generative AI | 基于 Amazon 扩散模型原理的代码实践之采样篇
  • [服务器-数据库]MongoDBv7.0.4不支持ipv6访问
  • 【b站咸虾米】chapter5_uniapp-API_新课uniapp零基础入门到项目打包(微信小程序/H5/vue/安卓apk)全掌握
  • 自学Python第十八天-自动化测试框架(二):DrissionPage、appium
  • 云尚办公-0.3.0
  • 汇编英文全称
  • 基于虚拟力优化的无线传感器网络覆盖率matlab仿真
  • 阿里云-系统盘-磁盘扩容
  • libmmd.dll修复
  • 大数据时代的明星助手:数据可视化引领新风潮
  • 设计模式--享元模式和组合模式
  • 基于Java springmvc+mybatis酒店信息管理系统设计和实现
  • leetcode-找不同
  • 笔记本hp6930p安装Android-x86避坑日记