diff --git a/src/3000-maximum-area-of-longest-diagonal-rectangle/main.go b/src/3000-maximum-area-of-longest-diagonal-rectangle/main.go new file mode 100644 index 0000000..1deb223 --- /dev/null +++ b/src/3000-maximum-area-of-longest-diagonal-rectangle/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "math" +) + +func areaOfMaxDiagonal(dimensions [][]int) int { + var area int + var maxArea int + var diagonal float64 + var maxDiagonal float64 + for i := range dimensions { + length := dimensions[i][0] + width := dimensions[i][1] + diagonal = math.Sqrt(float64(length)*float64(length) + float64(width)*float64(width)) + area = length * width + if diagonal > maxDiagonal { + maxDiagonal = diagonal + maxArea = area + } + if diagonal == maxDiagonal { + if area > maxArea { + maxArea = area + } + } + } + return maxArea +} diff --git a/src/3000-maximum-area-of-longest-diagonal-rectangle/main_test.go b/src/3000-maximum-area-of-longest-diagonal-rectangle/main_test.go new file mode 100644 index 0000000..1ace576 --- /dev/null +++ b/src/3000-maximum-area-of-longest-diagonal-rectangle/main_test.go @@ -0,0 +1,53 @@ +package main + +import ( + "testing" + + "github.com/arunsathiya/leetcode/testutil" +) + +func TestAreaOfMaxDiagonal(t *testing.T) { + tests := []struct { + name string + input string + expected int + }{ + { + name: "first test case from leetcode", + input: "[[9,3],[8,6]]", + expected: 48, + }, + { + name: "second test case from leetcode", + input: "[[3,4],[4,3]]", + expected: 12, + }, + { + name: "third test case from leetcode", + input: "[[6,5],[8,6],[2,10],[8,1],[9,2],[3,5],[3,5]]", + expected: 20, + }, + { + name: "fourth test case from leetcode", + input: "[[4,7],[8,9],[5,3],[6,10],[2,9],[3,10],[2,2],[5,8],[5,10],[5,6],[8,9],[10,7],[8,9],[3,7],[2,6],[5,1],[7,4],[1,10],[1,7],[6,9],[3,3],[4,6],[8,2],[10,6],[7,9],[9,2],[1,2],[3,8],[10,2],[4,1],[9,7],[10,3],[6,9],[9,8],[7,7],[5,7],[5,4],[6,5],[1,8],[2,3],[7,10],[3,9],[5,7],[2,4],[5,6],[9,5],[8,8],[8,10],[6,8],[5,1],[10,8],[7,4],[2,1],[2,7],[10,3],[2,5],[7,6],[10,5],[10,9],[5,7],[10,6],[4,3],[10,4],[1,5],[8,9],[3,1],[2,5],[9,10],[6,6],[5,10],[10,2],[6,10],[1,1],[8,6],[1,7],[6,3],[9,3],[1,4],[1,1],[10,4],[7,9],[4,5],[2,8],[7,9],[7,3],[4,9],[2,8],[4,6],[9,1],[8,4],[2,4],[7,8],[3,5],[7,6],[8,6],[4,7],[25,60],[39,52],[16,63],[33,56]]", + expected: 2028, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + dimensions := testutil.Parse2DIntArray(test.input) + output := areaOfMaxDiagonal(dimensions) + if output != test.expected { + t.Errorf("Expected %d, got %d", test.expected, output) + } + }) + } +} + +func BenchmarkAreaOfMaxDiagonal(b *testing.B) { + // TODO: add benchmark inputs + for i := 0; i < b.N; i++ { + // areaOfMaxDiagonal(...) + } +} diff --git a/testutil/leetcode.go b/testutil/leetcode.go new file mode 100644 index 0000000..7cb28c2 --- /dev/null +++ b/testutil/leetcode.go @@ -0,0 +1,16 @@ +package testutil + +import ( + "encoding/json" +) + +// Parse2DIntArray converts LeetCode format string to [][]int +// Example: "[[1,2],[3,4]]" -> [][]int{{1,2},{3,4}} +func Parse2DIntArray(jsonStr string) [][]int { + var result [][]int + err := json.Unmarshal([]byte(jsonStr), &result) + if err != nil { + panic("Failed to parse 2D int array: " + err.Error()) + } + return result +}