add: 220129

This commit is contained in:
Lam Haoyin 2022-02-01 22:30:43 +08:00
parent 696bf5a93e
commit 41677cc3df
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
1 changed files with 52 additions and 0 deletions

52
2201/220129.cpp Normal file
View File

@ -0,0 +1,52 @@
#include <vector>
#include <iostream>
#include <random>
#include <queue>
#include <ctime>
#include <chrono>
class Solution {
private:
// Interval [L, R)
static int dAC(const std::vector<int>& 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<int>& heights) {
return dAC(heights, 0, heights.size());
}
};
int main() {
std::vector<int> 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<int> 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<std::chrono::milliseconds>(end - start).count() << " ms.\n";
return 0;
}