Skip to content

Commit

Permalink
Add problem 1862: Sum of Floored Pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Apr 27, 2024
1 parent fd97db7 commit 5a8483e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,7 @@ pub mod problem_1857_largest_color_value_in_a_directed_graph;
pub mod problem_1859_sorting_the_sentence;
pub mod problem_1860_incremental_memory_leak;
pub mod problem_1861_rotating_the_box;
pub mod problem_1862_sum_of_floored_pairs;
pub mod problem_1864_minimum_number_of_swaps_to_make_the_binary_string_alternating;
pub mod problem_1865_finding_pairs_with_a_certain_sum;
pub mod problem_1869_longer_contiguous_segments_of_ones_than_zeros;
Expand Down
63 changes: 63 additions & 0 deletions src/problem_1862_sum_of_floored_pairs/iterative.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
pub fn sum_of_floored_pairs(nums: Vec<i32>) -> i32 {
let nums = nums.into_iter().map(|num| num as u32).collect::<Vec<_>>();
let max_num = nums.iter().fold(0, |x, &y| x.max(y));
let mut counts = vec![0_u32; max_num as usize].into_boxed_slice();

for num in nums {
counts[num as usize - 1] += 1;
}

let mut total_count = 0;

for count in &mut *counts {
total_count += *count;
*count = total_count;
}

let mut result = 0_u64;
let mut left_count = 0;

for (i, &right_count) in counts.iter().enumerate() {
if right_count != left_count {
let mut sum = 0;
let mut prev_count = left_count;
let mut quotient = 1;

if let Some(window) = counts.get(i * 2..) {
for &count in window.iter().step_by(i + 1) {
sum += u64::from(count - prev_count) * quotient;
prev_count = count;
quotient += 1;
}
}

sum += u64::from(total_count - prev_count) * quotient;
result += u64::from(right_count - left_count) * sum;
left_count = right_count;
}
}

(result % 1_000_000_007) as _
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn sum_of_floored_pairs(nums: Vec<i32>) -> i32 {
Self::sum_of_floored_pairs(nums)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
22 changes: 22 additions & 0 deletions src/problem_1862_sum_of_floored_pairs/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pub mod iterative;

pub trait Solution {
fn sum_of_floored_pairs(nums: Vec<i32>) -> i32;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [
(&[2, 5, 9] as &[_], 10),
(&[7, 7, 7, 7, 7, 7, 7], 49),
(&[4, 3, 4, 3, 5], 17),
];

for (nums, expected) in test_cases {
assert_eq!(S::sum_of_floored_pairs(nums.to_vec()), expected);
}
}
}

0 comments on commit 5a8483e

Please sign in to comment.