Leetcode 3000. Maximum Area of Longest Diagonal Rectangle
You are given a rectangle of dimensions n x m
. You need to find the maximum area of a rectangle that can be cut out from this rectangle such that the cut-out rectangle has the same aspect ratio as the original rectangle.
n
and m
?
n/m
.We observe that maintaining the aspect ratio of ( n/m ) means any rectangle we cut out of the given rectangle must match this ratio.
So, if our intention is to find the maximum possible area of a rectangle with the same aspect ratio, then it must be a portion of the original rectangle itself where both sides are scaled down but maintain the aspect ratio.
Given the constraints, the maximum area will actually be the area of the original rectangle itself because you can’t find a larger subrectangle maintaining the same ratio without scaling.
Here is how you’d implement this in C++:
#include <iostream>
int main() {
long long n, m;
std::cin >> n >> m;
long long maxArea = n * m;
std::cout << maxArea << std::endl;
return 0;
}
n
and m
as inputs which represent the dimensions of the given rectangle.This solution leverages simple arithmetic and basic I/O functionality to deliver the maximum area of a subrectangle cut out with the same aspect ratio as the original.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?