2552. 统计上升四元组
题目链接:2552. 统计上升四元组
代码如下:
//参考链接:https://leetcode.cn/problems/count-increasing-quadruplets/solutions/2080632/you-ji-qiao-de-mei-ju-yu-chu-li-pythonja-exja
class Solution
{
public:
long long countQuadruplets(vector<int>& nums)
{
vector<vector<int>> great(nums.size(), vector<int>(nums.size() + 1));
for (int k = nums.size() - 2; k >= 2; k--)
{
great[k] = great[k + 1];
for (int x = 1; x < nums[k + 1]; x++)
{
great[k][x]++;
}
}
long long res = 0;
vector<int> less(nums.size() + 1);
for (int j = 1; j < nums.size() - 2; j++)
{
for (int x = nums[j - 1] + 1; x <= nums.size(); x++)
{
less[x]++;
}
for (int k = j + 1; k < nums.size() - 1; k++)
{
if (nums[j] > nums[k])
{
res += less[nums[k]] * great[k][nums[j]];
}
}
}
return res;
}
};