#include #include #include #include #include #include class Solution { private: // Interval [L, R) static int dAC(const std::vector& heights, int L, int R) { if (R - L <= 1) return heights[L]; const auto minPos = std::distance(heights.begin(), std::min_element(heights.begin() + L, heights.begin() + R)); const auto minVal = heights[minPos]; int ans = (R - L) * minVal; if (minPos - L >= 1) { int nextR = minPos; while (nextR - L > 1 && heights[nextR - 1] <= minVal) --nextR; ans = std::max(ans, dAC(heights, L, nextR)); } if (R - minPos > 1) { int nextL = minPos + 1; while (R - nextL > 1 && heights[nextL] <= minVal) ++nextL; ans = std::max(ans, dAC(heights, nextL, R)); } return ans; } public: static int largestRectangleArea(const std::vector& heights) { return dAC(heights, 0, heights.size()); } }; int main() { std::vector sample{3, 6, 5, 7, 4, 8, 1, 0}; std::cout << Solution::largestRectangleArea(sample) << std::endl; const int LIM = 100000; std::mt19937 r(time(nullptr)); std::vector arg; arg.reserve(LIM); for (int i = 0; i < LIM; ++i) { arg.push_back(r() % 10000); } const auto start = std::chrono::high_resolution_clock::now(); std::cout << Solution::largestRectangleArea(arg) << std::endl; const auto end = std::chrono::high_resolution_clock::now(); std::cout << std::chrono::duration_cast(end - start).count() << " ms.\n"; return 0; }