From 79b9f4aadae823b64fb0faf721fd975837f8ef2e Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 7 Jan 2024 20:38:58 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.10035 No.10035.Maximum Area of Longest Diagonal Rectangle --- .../README.md | 78 ++++++++++++++++++- .../README_EN.md | 78 ++++++++++++++++++- .../Solution.cpp | 17 ++++ .../Solution.go | 14 ++++ .../Solution.java | 16 ++++ .../Solution.py | 11 +++ .../Solution.ts | 13 ++++ 7 files changed, 221 insertions(+), 6 deletions(-) create mode 100644 solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.cpp create mode 100644 solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.go create mode 100644 solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.java create mode 100644 solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.py create mode 100644 solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.ts diff --git a/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/README.md b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/README.md index 572f31398e032..d0469bce81885 100644 --- a/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/README.md +++ b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/README.md @@ -54,7 +54,17 @@ ```python - +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 ``` ### **Java** @@ -62,19 +72,81 @@ ```java - +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; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int areaOfMaxDiagonal(vector>& 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; + } +}; ``` ### **Go** ```go +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 +} +``` +### **TypeScript** + +```ts +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; +} ``` ### **...** diff --git a/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/README_EN.md b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/README_EN.md index 344d43c6d78f2..106c7aa8f4d36 100644 --- a/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/README_EN.md +++ b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/README_EN.md @@ -46,25 +46,97 @@ So, the rectangle at index 1 has a greater diagonal length therefore we return a ### **Python3** ```python - +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 ``` ### **Java** ```java - +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; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int areaOfMaxDiagonal(vector>& 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; + } +}; ``` ### **Go** ```go +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 +} +``` +### **TypeScript** + +```ts +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; +} ``` ### **...** diff --git a/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.cpp b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.cpp new file mode 100644 index 0000000000000..8a42eb8e05399 --- /dev/null +++ b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int areaOfMaxDiagonal(vector>& 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; + } +}; \ No newline at end of file diff --git a/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.go b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.go new file mode 100644 index 0000000000000..a217d6d0f8548 --- /dev/null +++ b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.go @@ -0,0 +1,14 @@ +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 +} \ No newline at end of file diff --git a/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.java b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.java new file mode 100644 index 0000000000000..2692caa29a9dc --- /dev/null +++ b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.java @@ -0,0 +1,16 @@ +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; + } +} \ No newline at end of file diff --git a/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.py b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.py new file mode 100644 index 0000000000000..ff0279628b623 --- /dev/null +++ b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.py @@ -0,0 +1,11 @@ +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 diff --git a/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.ts b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.ts new file mode 100644 index 0000000000000..3c97b1f2cabb1 --- /dev/null +++ b/solution/10000-10099/10035.Maximum Area of Longest Diagonal Rectangle/Solution.ts @@ -0,0 +1,13 @@ +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; +}