Skip to content

Commit d7ad1ca

Browse files
committed
Add problem 3014: Minimum Number of Pushes to Type Word I
1 parent 152d67e commit d7ad1ca

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,6 +2047,7 @@ pub mod problem_3005_count_elements_with_maximum_frequency;
20472047
pub mod problem_3010_divide_an_array_into_subarrays_with_minimum_cost_i;
20482048
pub mod problem_3011_find_if_array_can_be_sorted;
20492049
pub mod problem_3012_minimize_length_of_array_using_operations;
2050+
pub mod problem_3014_minimum_number_of_pushes_to_type_word_i;
20502051

20512052
#[cfg(test)]
20522053
mod test_utilities;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn minimum_pushes(word: String) -> i32 {
7+
let n = word.len();
8+
let quotient = n / 8;
9+
let remainder = n % 8;
10+
11+
(4 * (1 + quotient) * quotient + (quotient + 1) * remainder) as _
12+
}
13+
}
14+
15+
// ------------------------------------------------------ snip ------------------------------------------------------ //
16+
17+
impl super::Solution for Solution {
18+
fn minimum_pushes(word: String) -> i32 {
19+
Self::minimum_pushes(word)
20+
}
21+
}
22+
23+
#[cfg(test)]
24+
mod tests {
25+
#[test]
26+
fn test_solution() {
27+
super::super::tests::run::<super::Solution>();
28+
}
29+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod greedy;
2+
3+
pub trait Solution {
4+
fn minimum_pushes(word: String) -> i32;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [("abcde", 5), ("xycdefghij", 12)];
13+
14+
for (word, expected) in test_cases {
15+
assert_eq!(S::minimum_pushes(word.to_string()), expected);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)