Skip to content

Commit 976ae9d

Browse files
Update integer_partition.rs
1 parent bf22900 commit 976ae9d

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/dynamic_programming/integer_partition.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,19 @@
3333
/// assert_eq!(partition(7), 15);
3434
/// assert_eq!(partition(100), 190569292);
3535
/// ```
36+
#[allow(clippy::large_stack_arrays)]
3637
pub fn partition(m: i32) -> u128 {
3738
// Validate input
3839
assert!(m > 0, "Input must be a positive integer greater than 0");
3940

4041
let m = m as usize;
4142

42-
// Initialize memo table with zeros
43-
let mut memo = vec![vec![0u128; m]; m + 1];
43+
// Initialize memo table with zeros using iterative construction
44+
// to avoid large stack allocations
45+
let mut memo: Vec<Vec<u128>> = Vec::with_capacity(m + 1);
46+
for _ in 0..=m {
47+
memo.push(vec![0u128; m]);
48+
}
4449

4550
// Base case: there's one way to partition into 0 parts (empty partition)
4651
for i in 0..=m {
@@ -64,6 +69,7 @@ pub fn partition(m: i32) -> u128 {
6469
}
6570

6671
#[cfg(test)]
72+
#[allow(clippy::large_stack_arrays)]
6773
mod tests {
6874
use super::*;
6975

@@ -78,11 +84,13 @@ mod tests {
7884
}
7985

8086
#[test]
87+
#[allow(clippy::large_stack_arrays)]
8188
fn test_partition_100() {
8289
assert_eq!(partition(100), 190569292);
8390
}
8491

8592
#[test]
93+
#[allow(clippy::large_stack_arrays)]
8694
fn test_partition_1000() {
8795
assert_eq!(partition(1000), 24061467864032622473692149727991);
8896
}

0 commit comments

Comments
 (0)