-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3000-MaximumAreaOfLongestDiagonalRectangle.go
More file actions
81 lines (71 loc) · 3.09 KB
/
3000-MaximumAreaOfLongestDiagonalRectangle.go
File metadata and controls
81 lines (71 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
// 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.
// Constraints:
// 1 <= dimensions.length <= 100
// dimensions[i].length == 2
// 1 <= dimensions[i][0], dimensions[i][1] <= 100
import "fmt"
import "math"
//
func areaOfMaxDiagonal(dimensions [][]int) int {
maxDiagonalLength, index := float64(0), 0
calcDiagonalLength := func(width, length int) float64 { // 计算对角线
return math.Sqrt(float64(width * width) + float64(length * length))
}
maxDiagonalLength, index = calcDiagonalLength(dimensions[0][0], dimensions[0][1]), 0
for i := 1; i < len(dimensions); i++ {
t := calcDiagonalLength(dimensions[i][0], dimensions[i][1])
if t > maxDiagonalLength {
maxDiagonalLength, index = t, i
}
}
return dimensions[index][0] * dimensions[index][1]
}
func areaOfMaxDiagonal1(dimensions [][]int) int {
maxArea, maxDiagonal := 0, 0
for _, dim := range dimensions {
l, w := dim[0], dim[1]
currentArea, currentDiagonal := l * w, (l * l) + (w * w)
if currentDiagonal > maxDiagonal {
maxDiagonal = currentDiagonal
maxArea = currentArea
} else if currentDiagonal == maxDiagonal {
if currentArea > maxArea {
maxArea = currentArea
}
}
}
return maxArea
}
func main() {
// 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.
fmt.Println(areaOfMaxDiagonal([][]int{{9,3},{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.
fmt.Println(areaOfMaxDiagonal([][]int{{3,4},{4,3}})) // 12
fmt.Println(areaOfMaxDiagonal1([][]int{{9,3},{8,6}})) // 48
fmt.Println(areaOfMaxDiagonal1([][]int{{3,4},{4,3}})) // 12
}