LeetCode 2552. 统计上升四元组(线性dp)
原题链接
困难
作者:
autumn_0
,
2024-09-10 20:58:22
,
所有人可见
,
阅读 2
class Solution {
public long countQuadruplets(int[] nums) {
int n = nums.length;
int[][] great = new int[n][n + 1];
for(int k = n - 2; k >= 2; k -- ){
great[k] = great[k + 1].clone();
for(int x = 1; x < nums[k + 1]; x ++ ){
great[k][x] ++ ;
}
}
long ans = 0;
int[] less = new int[n + 1];
for(int j = 1; j < n - 2; j ++ ){
for(int x = nums[j - 1] + 1; x <= n; x ++ ){
less[x] ++ ;
}
for(int k = j + 1; k < n - 1; k ++ ){
if(nums[j] > nums[k]){
ans += less[nums[k]] * great[k][nums[j]];
}
}
}
return ans;
}
}