返回介绍

solution / 3000-3099 / 3000.Maximum Area of Longest Diagonal Rectangle / README_EN

发布于 2024-06-17 01:02:58 字数 4005 浏览 0 评论 0 收藏 0

3000. Maximum Area of Longest Diagonal Rectangle

中文文档

Description

You are given a 2D 0-indexed integer array dimensions.

For all indices i, 0 <= i < dimensions.length, dimensions[i][0] represents the length and dimensions[i][1] represents the width of the rectangle i.

Return _the area of the rectangle having the longest diagonal. If there are multiple rectangles with the longest diagonal, return the area of the rectangle having the maximum area._

 

Example 1:

Input: dimensions = [[9,3],[8,6]]
Output: 48
Explanation: 
For index = 0, length = 9 and width = 3. Diagonal length = sqrt(9 * 9 + 3 * 3) = sqrt(90) ≈ 9.487.
For index = 1, length = 8 and width = 6. Diagonal length = sqrt(8 * 8 + 6 * 6) = sqrt(100) = 10.
So, the rectangle at index 1 has a greater diagonal length therefore we return area = 8 * 6 = 48.

Example 2:

Input: dimensions = [[3,4],[4,3]]
Output: 12
Explanation: Length of diagonal is the same for both which is 5, so maximum area = 12.

 

Constraints:

  • 1 <= dimensions.length <= 100
  • dimensions[i].length == 2
  • 1 <= dimensions[i][0], dimensions[i][1] <= 100

Solutions

Solution 1

class Solution:
  def areaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int:
    ans = mx = 0
    for l, w in dimensions:
      t = l**2 + w**2
      if mx < t:
        mx = t
        ans = l * w
      elif mx == t:
        ans = max(ans, l * w)
    return ans
class Solution {
  public int areaOfMaxDiagonal(int[][] dimensions) {
    int ans = 0, mx = 0;
    for (var d : dimensions) {
      int l = d[0], w = d[1];
      int t = l * l + w * w;
      if (mx < t) {
        mx = t;
        ans = l * w;
      } else if (mx == t) {
        ans = Math.max(ans, l * w);
      }
    }
    return ans;
  }
}
class Solution {
public:
  int areaOfMaxDiagonal(vector<vector<int>>& dimensions) {
    int ans = 0, mx = 0;
    for (auto& d : dimensions) {
      int l = d[0], w = d[1];
      int t = l * l + w * w;
      if (mx < t) {
        mx = t;
        ans = l * w;
      } else if (mx == t) {
        ans = max(ans, l * w);
      }
    }
    return ans;
  }
};
func areaOfMaxDiagonal(dimensions [][]int) (ans int) {
  mx := 0
  for _, d := range dimensions {
    l, w := d[0], d[1]
    t := l*l + w*w
    if mx < t {
      mx = t
      ans = l * w
    } else if mx == t {
      ans = max(ans, l*w)
    }
  }
  return
}
function areaOfMaxDiagonal(dimensions: number[][]): number {
  let [ans, mx] = [0, 0];
  for (const [l, w] of dimensions) {
    const t = l * l + w * w;
    if (mx < t) {
      mx = t;
      ans = l * w;
    } else if (mx === t) {
      ans = Math.max(ans, l * w);
    }
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文