20250113 finished.
This commit is contained in:
parent
676b28a67e
commit
e42ada3e01
|
@ -428,3 +428,5 @@ mod p3298_count_substrings_that_can_be_rearranged_to_contain_a_string_ii;
|
|||
mod p3270_find_the_key_of_the_numbers;
|
||||
|
||||
mod p2275_largest_combination_with_bitwise_and_greater_than_zero;
|
||||
|
||||
mod p2270_number_of_ways_to_split_array;
|
||||
|
|
39
src/problem/p2270_number_of_ways_to_split_array.rs
Normal file
39
src/problem/p2270_number_of_ways_to_split_array.rs
Normal file
|
@ -0,0 +1,39 @@
|
|||
/**
|
||||
* [2270] Number of Ways to Split Array
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn ways_to_split_array(nums: Vec<i32>) -> i32 {
|
||||
let nums: Vec<i64> = nums.into_iter().map(|x| x as i64).collect();
|
||||
let sum = nums.iter().sum::<i64>();
|
||||
|
||||
let mut current = 0;
|
||||
let mut result = 0;
|
||||
|
||||
for i in 0..nums.len() - 1 {
|
||||
current += nums[i];
|
||||
|
||||
if current * 2 >= sum {
|
||||
result += 1;
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_2270() {
|
||||
assert_eq!(2, Solution::ways_to_split_array(vec![10, 4, -8, 7]));
|
||||
assert_eq!(2, Solution::ways_to_split_array(vec![2, 3, 1, 0]));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user