-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1862: Sum of Floored Pairs
- Loading branch information
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |