Skip to content

Commit 15bbeee

Browse files
committed
202210212034
1 parent 0aa2683 commit 15bbeee

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

independent/202210212034/0.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[3, 1, 9, 4, 5, 6, 7, 0, 2]

independent/202210212034/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
给定一个整数数组a,元素各不相同,求max(j-i)且a[j]>a[i],要求复杂度最多O N

independent/202210212034/main.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
3+
*/
4+
5+
#include <bits/stdc++.h>
6+
#include "../../lib/LeetCodeInputTemplate.hpp"
7+
#include "solution.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.solution(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+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
3+
time: O()
4+
space: O()
5+
6+
Runtime:
7+
Memory Usage:
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 solution(vi a) {
21+
int n = size(a);
22+
queue<pair<int,int>> x;
23+
stack<pair<int,int>> y;
24+
int min = a[0];
25+
int max = a[n-1];
26+
x.emplace(min, 0);
27+
y.emplace(max, n-1);
28+
for (int i=1; i<n; ++i){
29+
if (a[i]<min){
30+
min = a[i];
31+
x.emplace(min, i);
32+
}
33+
}
34+
for (int i=n-1; i>0; --i){
35+
if (a[i]>max){
36+
max = a[i];
37+
x.emplace(max, i);
38+
}
39+
}
40+
int ans = -2147483648;
41+
while (!x.empty() && !y.empty()){
42+
if (x.front().first > y.top().first){
43+
x.pop();
44+
} else {
45+
ans = std::max(ans, y.top().second - x.front().second);
46+
y.pop();
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+
}();

0 commit comments

Comments
 (0)