3000. Maximum Area of Longest Diagonal Rectangle
⭐
題目敘述
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.
解題思路
基本上按照題目邏輯完成函式就可以。
Solution
class Solution {
public int areaOfMaxDiagonal(int[][] dimensions) {
int result = 0;
double max_area = 0;
for (int i = 0; i < dimensions.length; i++) {
double area = dimensions[i][0] * dimensions[i][0] + dimensions[i][1] * dimensions[i][1];
area = Math.sqrt(area);
if (area > max_area) {
max_area = area;
result = dimensions[i][0] * dimensions[i][1];
}else if (area == max_area) {
result = Math.max(result, dimensions[i][0] * dimensions[i][1]);
}
}
return result;
}
}
#include <vector>
#include <cmath>
#include <algorithm>
class Solution {
public:
int areaOfMaxDiagonal(std::vector<std::vector<int>>& dimensions) {
int max_area = 0;
int result = 0;
for (int i = 0; i < dimensions.size(); i++) {
double area = sqrt(pow(dimensions[i][0], 2) + pow(dimensions[i][1], 2));
if (area > max_area) {
max_area = area;
result = dimensions[i][0] * dimensions[i][1];
} else if (area == max_area) {
result = std::max(result, dimensions[i][0] * dimensions[i][1]);
}
}
return result;
}
};