20240910 finished.
This commit is contained in:
parent
f4dcf1a23d
commit
d03c1058c1
|
@ -231,3 +231,4 @@ mod p3176_find_the_maximum_length_of_a_good_subsequence_i;
|
|||
mod p3177_find_the_maximum_length_of_a_good_subsequence_ii;
|
||||
mod p977_squares_of_a_sorted_array;
|
||||
mod p2181_merge_nodes_in_between_zeros;
|
||||
mod p2552_count_increasing_quadruplets;
|
46
src/problem/p2552_count_increasing_quadruplets.rs
Normal file
46
src/problem/p2552_count_increasing_quadruplets.rs
Normal file
|
@ -0,0 +1,46 @@
|
|||
/**
|
||||
* [2552] Count Increasing Quadruplets
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn count_quadruplets(nums: Vec<i32>) -> i64 {
|
||||
let nums: Vec<usize> = nums.iter().map(|x| *x as usize).collect();
|
||||
let n = nums.len();
|
||||
let mut pre = vec![0;n + 1];
|
||||
let mut result = 0;
|
||||
|
||||
for j in 0..n {
|
||||
let mut suffix = 0;
|
||||
|
||||
for k in (j + 1..n).rev() {
|
||||
if nums[j] > nums[k] {
|
||||
result += pre[nums[k]] * suffix;
|
||||
} else {
|
||||
suffix += 1;
|
||||
}
|
||||
}
|
||||
|
||||
for i in nums[j] + 1..=n {
|
||||
pre[i] += 1;
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_2552() {
|
||||
assert_eq!(2, Solution::count_quadruplets(vec![1,3,2,4,5]));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user