单调栈
1 int main() { 2 vector<int> nums{ 5,4,2,6,7,3,9 }; 3 int length = nums.size(); 4 5 stack<vector<int>>sk1; 6 vector<int>rightFirstMax(length, 0); 7 for (int index = length - 1; index >= 0; index--) { 8 while (!sk1.empty() && nums[index] > sk1.top()[0]) { 9 sk1.pop(); 10 } 11 if (!sk1.empty()) { 12 rightFirstMax[index] = sk1.top()[1]; 13 } 14 vector<int>temp(2, 0); 15 temp[0] = nums[index]; 16 temp[1] = index; 17 sk1.push(temp); 18 } 19 20 vector<int>leftFirstMin(length, 0); 21 stack<vector<int>>sk2; 22 for (int index = 0; index < length; index++) { 23 while (!sk2.empty() && nums[index] < sk2.top()[0]) { 24 sk2.pop(); 25 } 26 if (!sk2.empty()) { 27 leftFirstMin[index] = sk2.top()[1]; 28 } 29 vector<int>temp(2, 0); 30 temp[0] = nums[index]; 31 temp[1] = index; 32 sk2.push(temp); 33 } 34 35 int res = 0; 36 for (int i = 0; i < length; ++i) { 37 int temp = rightFirstMax[i] * leftFirstMin[i]; 38 if (temp > res) { 39 res = temp; 40 } 41 } 42 cout << res; 43 return 0; 44 }