File tree Expand file tree Collapse file tree 1 file changed +10
-2
lines changed
Expand file tree Collapse file tree 1 file changed +10
-2
lines changed Original file line number Diff line number Diff line change 3333/// assert_eq!(partition(7), 15);
3434/// assert_eq!(partition(100), 190569292);
3535/// ```
36+ #[ allow( clippy:: large_stack_arrays) ]
3637pub 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) ]
6773mod 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 }
You can’t perform that action at this time.
0 commit comments