diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 01387a7..40208f8 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -230,4 +230,5 @@ mod p3174_clear_digits; 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; \ No newline at end of file +mod p2181_merge_nodes_in_between_zeros; +mod p2552_count_increasing_quadruplets; \ No newline at end of file diff --git a/src/problem/p2552_count_increasing_quadruplets.rs b/src/problem/p2552_count_increasing_quadruplets.rs new file mode 100644 index 0000000..bdfa9a5 --- /dev/null +++ b/src/problem/p2552_count_increasing_quadruplets.rs @@ -0,0 +1,46 @@ +/** + * [2552] Count Increasing Quadruplets + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn count_quadruplets(nums: Vec) -> i64 { + let nums: Vec = 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])); + } +}