Skip to content

Commit ef36c97

Browse files
committed
test: 3000 solution
py, c++, go, java
1 parent e6031dd commit ef36c97

File tree

4 files changed

+56
-19
lines changed

4 files changed

+56
-19
lines changed

problems/problems_3000/Solution.cpp

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
//go:build ignore
22
#include "cpp/common/Solution.h"
33

4-
54
using namespace std;
65
using json = nlohmann::json;
76

87
class Solution {
98
public:
10-
int areaOfMaxDiagonal(vector<vector<int>>& dimensions) {
11-
9+
int areaOfMaxDiagonal(const vector<vector<int>> &dimensions) {
10+
int max_length = 0, max_area = 0;
11+
for (const auto &dim : dimensions) {
12+
int a = dim[0], b = dim[1];
13+
int length = a * a + b * b;
14+
if (length > max_length) {
15+
max_length = length;
16+
max_area = a * b;
17+
} else if (length == max_length) {
18+
max_area = max(max_area, a * b);
19+
}
1220
}
21+
return max_area;
22+
}
1323
};
1424

1525
json leetcode::qubh::Solve(string input_json_values) {
16-
vector<string> inputArray;
17-
size_t pos = input_json_values.find('\n');
18-
while (pos != string::npos) {
19-
inputArray.push_back(input_json_values.substr(0, pos));
20-
input_json_values = input_json_values.substr(pos + 1);
21-
pos = input_json_values.find('\n');
22-
}
23-
inputArray.push_back(input_json_values);
26+
vector<string> inputArray;
27+
size_t pos = input_json_values.find('\n');
28+
while (pos != string::npos) {
29+
inputArray.push_back(input_json_values.substr(0, pos));
30+
input_json_values = input_json_values.substr(pos + 1);
31+
pos = input_json_values.find('\n');
32+
}
33+
inputArray.push_back(input_json_values);
2434

25-
Solution solution;
26-
vector<vector<int>> dimensions = json::parse(inputArray.at(0));
27-
return solution.areaOfMaxDiagonal(dimensions);
35+
Solution solution;
36+
vector<vector<int>> dimensions = json::parse(inputArray.at(0));
37+
return solution.areaOfMaxDiagonal(dimensions);
2838
}

problems/problems_3000/Solution.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,18 @@
77

88
public class Solution extends BaseSolution {
99
public int areaOfMaxDiagonal(int[][] dimensions) {
10-
10+
int maxLength = 0, maxArea = 0;
11+
for (int[] dim: dimensions) {
12+
int a = dim[0], b = dim[1];
13+
int length = a * a + b * b;
14+
if (length > maxLength) {
15+
maxLength = length;
16+
maxArea = a * b;
17+
} else if (length == maxLength) {
18+
maxArea = Math.max(maxArea, a * b);
19+
}
20+
}
21+
return maxArea;
1122
}
1223

1324
@Override

problems/problems_3000/solution.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,18 @@ import (
66
"strings"
77
)
88

9-
func areaOfMaxDiagonal(dimensions [][]int) int {
10-
9+
func areaOfMaxDiagonal(dimensions [][]int) (ans int) {
10+
maxLength := 0
11+
for _, dimension := range dimensions {
12+
a, b := dimension[0], dimension[1]
13+
if length := a*a + b*b; length > maxLength {
14+
maxLength = length
15+
ans = a * b
16+
} else if length == maxLength {
17+
ans = max(ans, a*b)
18+
}
19+
}
20+
return
1121
}
1222

1323
func Solve(inputJsonValues string) any {

problems/problems_3000/solution.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,11 @@ def solve(self, test_input=None):
77
return self.areaOfMaxDiagonal(test_input)
88

99
def areaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int:
10-
pass
11-
10+
max_length, max_area = 0, 0
11+
for a, b in dimensions:
12+
if (length := a * a + b * b) > max_length:
13+
max_length = length
14+
max_area = a * b
15+
elif length == max_length:
16+
max_area = max(max_area, a * b)
17+
return max_area

0 commit comments

Comments
 (0)