File tree Expand file tree Collapse file tree 8 files changed +160
-0
lines changed
Expand file tree Collapse file tree 8 files changed +160
-0
lines changed Original file line number Diff line number Diff line change 1+ [2,1,5,6,2,3]
Original file line number Diff line number Diff line change 1+ [2,4]
Original file line number Diff line number Diff line change 1+ [1,1]
Original file line number Diff line number Diff line change 1+ [2,1,2]
Original file line number Diff line number Diff line change 1+ [4,2,0,3,2,5]
Original file line number Diff line number Diff line change 1+ /*
2+ 3+ */
4+
5+ #include < bits/stdc++.h>
6+ #include " ../../lib/LeetCodeInputTemplate.hpp"
7+ #include " solution-2.cpp"
8+ using namespace std ;
9+
10+ typedef vector<int > vi;
11+ typedef vector<vector<int >> vvi;
12+
13+ int main ()
14+ {
15+ LeetCodeInput li (" 0.in" );
16+
17+ auto l0 = li.get <vi>(0 );
18+
19+ chrono::steady_clock::time_point begin = chrono::steady_clock::now ();
20+ Solution solution;
21+ auto output = solution.largestRectangleArea (l0);
22+ chrono::steady_clock::time_point end = chrono::steady_clock::now ();
23+ cout << " Time difference = " << chrono::duration_cast<chrono::microseconds> (end - begin).count () << " µs" << endl;
24+
25+ cout << output << endl;
26+
27+ // for (int i: output){
28+ // cout << i << ' ';
29+ // }
30+
31+ // for (auto i: output){
32+ // for (int j: i){
33+ // cout << j << ' ';
34+ // }
35+ // cout << '\n';
36+ // }
37+
38+ return 0 ;
39+ }
Original file line number Diff line number Diff line change 1+ /*
2+ 3+ time: O(n)
4+ space: O(n)
5+
6+ Runtime: 153 ms, faster than 99.36% of C++ online submissions for Largest Rectangle in Histogram.
7+ Memory Usage: 76.5 MB, less than 77.82% of C++ online submissions for Largest Rectangle in Histogram.
8+ */
9+
10+ #include < bits/stdc++.h>
11+
12+ using namespace std ;
13+
14+ #define all (x ) begin(x), end(x)
15+ typedef vector<int > vi;
16+ typedef vector<vector<int >> vvi;
17+
18+ class Solution {
19+ public:
20+ int largestRectangleArea (vector<int >& heights) {
21+ int ans = 0 ;
22+ stack<pair<int ,int >> ms;
23+ int n = size (heights);
24+ // int ls[n];
25+ int rs[n];
26+
27+ for (int i=n-1 ; i>=0 ; --i){
28+ int height = heights[i];
29+ while (!ms.empty () && ms.top ().first >=height){
30+ ms.pop ();
31+ }
32+ rs[i] = ms.empty () ? n : ms.top ().second ;
33+ ms.emplace (height,i);
34+ }
35+
36+ ms = {};
37+
38+ for (int i=0 ; i<n; ++i){
39+ int height = heights[i];
40+ while (!ms.empty () && ms.top ().first >=height){
41+ ms.pop ();
42+ }
43+ // ls[i] = ms.empty() ? -1 : ms.top().second;
44+ int l = ms.empty () ? -1 : ms.top ().second ;
45+ ans = max (ans, height*(rs[i]-l-1 ));
46+ ms.emplace (height,i);
47+ }
48+
49+ return ans;
50+ }
51+ };
52+
53+ const static auto initialize = [] {
54+ std::ios::sync_with_stdio (false );
55+ std::cin.tie (nullptr );
56+ std::cout.tie (nullptr );
57+ return nullptr ;
58+ }();
Original file line number Diff line number Diff line change 1+ /*
2+ 3+ time: O(n)
4+ space: O(n)
5+
6+ Runtime: 157 ms, faster than 99.08% of C++ online submissions for Largest Rectangle in Histogram.
7+ Memory Usage: 76.9 MB, less than 77.52% of C++ online submissions for Largest Rectangle in Histogram.
8+ */
9+
10+ #include < bits/stdc++.h>
11+
12+ using namespace std ;
13+
14+ #define all (x ) begin(x), end(x)
15+ typedef vector<int > vi;
16+ typedef vector<vector<int >> vvi;
17+
18+ class Solution {
19+ public:
20+ int largestRectangleArea (vector<int >& heights) {
21+ int ans = 0 ;
22+ stack<pair<int ,int >> ms;
23+ int n = size (heights);
24+ int ls[n];
25+ int rs[n];
26+
27+ for (int i=n-1 ; i>=0 ; --i){
28+ int height = heights[i];
29+ while (!ms.empty () && ms.top ().first >=height){
30+ ms.pop ();
31+ }
32+ rs[i] = ms.empty () ? n : ms.top ().second ;
33+ ms.emplace (height,i);
34+ }
35+
36+ ms = {};
37+
38+ for (int i=0 ; i<n; ++i){
39+ int height = heights[i];
40+ while (!ms.empty () && ms.top ().first >=height){
41+ ms.pop ();
42+ }
43+ ls[i] = ms.empty () ? -1 : ms.top ().second ;
44+ int l = ms.empty () ? -1 : ms.top ().second ;
45+ ans = max (ans, height*(rs[i]-l-1 ));
46+ ms.emplace (height,i);
47+ }
48+
49+ return ans;
50+ }
51+ };
52+
53+ const static auto initialize = [] {
54+ std::ios::sync_with_stdio (false );
55+ std::cin.tie (nullptr );
56+ std::cout.tie (nullptr );
57+ return nullptr ;
58+ }();
You can’t perform that action at this time.
0 commit comments