-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
minimum_cost_path.rs
177 lines (167 loc) · 4.7 KB
/
minimum_cost_path.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use std::cmp::min;
/// Represents possible errors that can occur when calculating the minimum cost path in a matrix.
#[derive(Debug, PartialEq, Eq)]
pub enum MatrixError {
/// Error indicating that the matrix is empty or has empty rows.
EmptyMatrix,
/// Error indicating that the matrix is not rectangular in shape.
NonRectangularMatrix,
}
/// Computes the minimum cost path from the top-left to the bottom-right
/// corner of a matrix, where movement is restricted to right and down directions.
///
/// # Arguments
///
/// * `matrix` - A 2D vector of positive integers, where each element represents
/// the cost to step on that cell.
///
/// # Returns
///
/// * `Ok(usize)` - The minimum path cost to reach the bottom-right corner from
/// the top-left corner of the matrix.
/// * `Err(MatrixError)` - An error if the matrix is empty or improperly formatted.
///
/// # Complexity
///
/// * Time complexity: `O(m * n)`, where `m` is the number of rows
/// and `n` is the number of columns in the input matrix.
/// * Space complexity: `O(n)`, as only a single row of cumulative costs
/// is stored at any time.
pub fn minimum_cost_path(matrix: Vec<Vec<usize>>) -> Result<usize, MatrixError> {
// Check if the matrix is rectangular
if !matrix.iter().all(|row| row.len() == matrix[0].len()) {
return Err(MatrixError::NonRectangularMatrix);
}
// Check if the matrix is empty or contains empty rows
if matrix.is_empty() || matrix.iter().all(|row| row.is_empty()) {
return Err(MatrixError::EmptyMatrix);
}
// Initialize the first row of the cost vector
let mut cost = matrix[0]
.iter()
.scan(0, |acc, &val| {
*acc += val;
Some(*acc)
})
.collect::<Vec<_>>();
// Process each row from the second to the last
for row in matrix.iter().skip(1) {
// Update the first element of cost for this row
cost[0] += row[0];
// Update the rest of the elements in the current row of cost
for col in 1..matrix[0].len() {
cost[col] = row[col] + min(cost[col - 1], cost[col]);
}
}
// The last element in cost contains the minimum path cost to the bottom-right corner
Ok(cost[matrix[0].len() - 1])
}
#[cfg(test)]
mod tests {
use super::*;
macro_rules! minimum_cost_path_tests {
($($name:ident: $test_case:expr,)*) => {
$(
#[test]
fn $name() {
let (matrix, expected) = $test_case;
assert_eq!(minimum_cost_path(matrix), expected);
}
)*
};
}
minimum_cost_path_tests! {
basic: (
vec![
vec![2, 1, 4],
vec![2, 1, 3],
vec![3, 2, 1]
],
Ok(7)
),
single_element: (
vec![
vec![5]
],
Ok(5)
),
single_row: (
vec![
vec![1, 3, 2, 1, 5]
],
Ok(12)
),
single_column: (
vec![
vec![1],
vec![3],
vec![2],
vec![1],
vec![5]
],
Ok(12)
),
large_matrix: (
vec![
vec![1, 3, 1, 5],
vec![2, 1, 4, 2],
vec![3, 2, 1, 3],
vec![4, 3, 2, 1]
],
Ok(10)
),
uniform_matrix: (
vec![
vec![1, 1, 1],
vec![1, 1, 1],
vec![1, 1, 1]
],
Ok(5)
),
increasing_values: (
vec![
vec![1, 2, 3],
vec![4, 5, 6],
vec![7, 8, 9]
],
Ok(21)
),
high_cost_path: (
vec![
vec![1, 100, 1],
vec![1, 100, 1],
vec![1, 1, 1]
],
Ok(5)
),
complex_matrix: (
vec![
vec![5, 9, 6, 8],
vec![1, 4, 7, 3],
vec![2, 1, 8, 2],
vec![3, 6, 9, 4]
],
Ok(23)
),
empty_matrix: (
vec![],
Err(MatrixError::EmptyMatrix)
),
empty_row: (
vec![
vec![],
vec![],
vec![]
],
Err(MatrixError::EmptyMatrix)
),
non_rectangular: (
vec![
vec![1, 2, 3],
vec![4, 5],
vec![6, 7, 8]
],
Err(MatrixError::NonRectangularMatrix)
),
}
}