Skip to content

Commit daeb6bf

Browse files
committed
Add problem 3028: Ant on the Boundary
1 parent b93b9e0 commit daeb6bf

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,6 +2053,7 @@ pub mod problem_3019_number_of_changing_keys;
20532053
pub mod problem_3021_alice_and_bob_playing_flower_game;
20542054
pub mod problem_3024_type_of_triangle;
20552055
pub mod problem_3026_maximum_good_subarray_sum;
2056+
pub mod problem_3028_ant_on_the_boundary;
20562057

20572058
#[cfg(test)]
20582059
mod test_utilities;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod prefix_sums;
2+
3+
pub trait Solution {
4+
fn return_to_boundary_count(nums: Vec<i32>) -> i32;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [(&[2, 3, -5] as &[_], 1), (&[3, 2, -3, -4], 0)];
13+
14+
for (nums, expected) in test_cases {
15+
assert_eq!(S::return_to_boundary_count(nums.to_vec()), expected);
16+
}
17+
}
18+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn return_to_boundary_count(nums: Vec<i32>) -> i32 {
7+
let mut position = 0;
8+
let mut result = 0;
9+
10+
for num in nums {
11+
position += num;
12+
result += i32::from(position == 0);
13+
}
14+
15+
result
16+
}
17+
}
18+
19+
// ------------------------------------------------------ snip ------------------------------------------------------ //
20+
21+
impl super::Solution for Solution {
22+
fn return_to_boundary_count(nums: Vec<i32>) -> i32 {
23+
Self::return_to_boundary_count(nums)
24+
}
25+
}
26+
27+
#[cfg(test)]
28+
mod tests {
29+
#[test]
30+
fn test_solution() {
31+
super::super::tests::run::<super::Solution>();
32+
}
33+
}

0 commit comments

Comments
 (0)