feat: add initial implementation of Cargo.toml, Cargo.lock, and lib.rs with min_operations function
This commit is contained in:
parent
610d05ddb2
commit
7848468524
7
1769_minimum_number_of_operations_to_move_all_balls_to_each_box/Cargo.lock
generated
Normal file
7
1769_minimum_number_of_operations_to_move_all_balls_to_each_box/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "minimum_number_of_operations_to_move_all_balls_to_each_box"
|
||||
version = "0.1.0"
|
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "minimum_number_of_operations_to_move_all_balls_to_each_box"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
@ -0,0 +1,33 @@
|
||||
pub struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn min_operations(boxes: String) -> Vec<i32> {
|
||||
let boxes: Vec<i32> = boxes.chars().map(|c| c as i32 - 48).collect();
|
||||
|
||||
let mut balls_counted_from_left = vec![0; boxes.len()];
|
||||
for i in 1..boxes.len() {
|
||||
balls_counted_from_left[i] = balls_counted_from_left[i - 1] + boxes[i - 1];
|
||||
}
|
||||
|
||||
let mut balls_counted_from_right = vec![0; boxes.len()];
|
||||
for i in (0..(boxes.len() - 1)).rev() {
|
||||
balls_counted_from_right[i] = balls_counted_from_right[i + 1] + boxes[i + 1];
|
||||
}
|
||||
|
||||
let mut steps_moving_to_right = vec![0; boxes.len()];
|
||||
for i in 1..boxes.len() {
|
||||
steps_moving_to_right[i] = steps_moving_to_right[i - 1] + balls_counted_from_left[i];
|
||||
}
|
||||
|
||||
let mut steps_moving_to_left = vec![0; boxes.len()];
|
||||
for i in (0..(boxes.len() - 1)).rev() {
|
||||
steps_moving_to_left[i] = steps_moving_to_left[i + 1] + balls_counted_from_right[i];
|
||||
}
|
||||
|
||||
steps_moving_to_left
|
||||
.iter()
|
||||
.zip(steps_moving_to_right.iter())
|
||||
.map(|(&l, &r)| l + r)
|
||||
.collect()
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user