Cho một mảng các số nguyên heights biểu thị chiều cao của các cột trong biểu đồ tần số, trong đó chiều rộng của mỗi cột là 1, hãy trả về diện tích của hình chữ nhật lớn nhất trong biểu đồ đó.
Example 1:
Đầu vào: heights = [2,1,5,6,2,3]
Đầu ra: 10
Giải thích: Trên đây là biểu đồ cột với chiều rộng của mỗi cột là 1.
Hình chữ nhật lớn nhất được hiển thị trong vùng màu đỏ, có diện tích = 10 đơn vị.
Example 2:
Đầu vào: heights = [2,4]
Đầu ra: 4
Ràng buộc
- 1 <= heights.length <= 105
- 0 <= heights[i] <= 104
Solution Stats
Problem Comments
6 Comments
Solution Comments
Show comments
Loading...
Problem Recent Solvers5
Suggested Problems
More from this Author14
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Quite a few of the tests appear to be incorrect.
I agree with Christian, test cases 2, 8, 14, 22, 24, 26, and 30 are incorrect.
Yes, I agree. In fact, test 2 is used as an example in the statement of the problem, and the answer is listed as 4. But in test 2, the correct answer is said to be 6.
By the way, you can quickly check that some of the tests are wrong just by factorizing the supposed correct area. For example, in test 2, if 6 were the correct result, there would have to be a rectangle of height 3 and width 2, or a rectangle of height 2 and width 3. Neither of these works. This test is very handy for the longer histograms.
I found the commonality between the erroneous test cases and thus was able to modify my code to get past them:
heights=[max(heights),heights];
Looking at Sune's solution, it would seem the "window" (or width of the box) is one more than it needs to be if it encompasses the first height value.
Thanks, Matthew!