-
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 1802: Maximum Value at a Given Index in a Bounded Array
- Loading branch information
Showing
3 changed files
with
68 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
49 changes: 49 additions & 0 deletions
49
src/problem_1802_maximum_value_at_a_given_index_in_a_bounded_array/mathematical.rs
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,49 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
fn get_area(start: u64, end: u64) -> u64 { | ||
(start + end) * (end + 1 - start) / 2 | ||
} | ||
|
||
#[allow(clippy::cast_precision_loss)] | ||
pub fn max_value(n: i32, index: i32, max_sum: i32) -> i32 { | ||
let n = n as u32; | ||
let index = index as u32; | ||
let index = index.min(n - 1 - index); | ||
let max_sum = max_sum as u32 - n; | ||
let n_u64 = u64::from(n); | ||
let index_u64 = u64::from(index); | ||
let max_sum_u64 = u64::from(max_sum); | ||
|
||
(if max_sum_u64 <= (index_u64 + 1).pow(2) { | ||
f64::from(max_sum).sqrt() as u32 | ||
} else { | ||
let threshold = | ||
Self::get_area(1, n_u64 - index_u64) + Self::get_area(n_u64 - index_u64 * 2, n_u64 - index_u64 - 1); | ||
|
||
if max_sum_u64 <= threshold { | ||
(f64::sqrt((8 * (max_sum_u64 + index_u64 * (index_u64 + 1)) + 1) as _) as u32 - index * 2 - 1) / 2 | ||
} else { | ||
n - index + (max_sum - threshold as u32) / n | ||
} | ||
} + 1) as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn max_value(n: i32, index: i32, max_sum: i32) -> i32 { | ||
Self::max_value(n, index, max_sum) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/problem_1802_maximum_value_at_a_given_index_in_a_bounded_array/mod.rs
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,18 @@ | ||
pub mod mathematical; | ||
|
||
pub trait Solution { | ||
fn max_value(n: i32, index: i32, max_sum: i32) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [((4, 2, 6), 2), ((6, 1, 10), 3), ((4, 0, 4), 1), ((5, 0, 28), 7)]; | ||
|
||
for ((n, index, max_sum), expected) in test_cases { | ||
assert_eq!(S::max_value(n, index, max_sum), expected); | ||
} | ||
} | ||
} |