标签:closed close color assert code cto open mamicode private
要求
示例
实现
1 class Solution { 2 private: 3 int max3( int a , int b , int c ){ 4 return max( a , max(b,c) ); 5 } 6 7 // 将n进行分割(至少两部分)可获得的最大乘积 8 int breakInteger(int n){ 9 10 if( n == 1 ) 11 return 1; 12 13 int res = -1; 14 for( int i = 1 ; i <= n-1 ; i ++ ) 15 // i + (n-i) 16 res = max3( res, i * (n-i) , i * breakInteger(n-i) ); 17 18 return res; 19 } 20 21 public: 22 int integerBreak(int n) { 23 return breakInteger(n); 24 } 25 };
1 class Solution { 2 private: 3 vector<int> memo; 4 5 int max3( int a , int b , int c ){ 6 return max( a , max(b,c) ); 7 } 8 9 // 将n进行分割(至少两部分)可获得的最大乘积 10 int breakInteger(int n){ 11 12 if( n == 1 ) 13 return 1; 14 15 if( memo[n] != -1) 16 return memo[n]; 17 18 int res = -1; 19 for( int i = 1 ; i <= n-1 ; i ++ ) 20 // i + (n-i) 21 res = max3( res, i * (n-i) , i * breakInteger(n-i) ); 22 memo[n] = res; 23 return res; 24 } 25 26 public: 27 int integerBreak(int n) { 28 memo = vector<int>(n+1,-1); 29 return breakInteger(n); 30 } 31 };
1 class Solution { 2 private: 3 int max3( int a , int b , int c ){ 4 return max( a , max(b,c) ); 5 } 6 7 public: 8 int integerBreak(int n) { 9 assert( n >= 2 ); 10 11 // memo[i]表示至少将数字i分割(至少两部分)后得到的最大乘积 12 vector<int> memo(n+1,-1); 13 14 memo[1] = 1; 15 for( int i = 2 ; i <= n ; i ++ ) 16 // 求解memo[j] 17 for( int j = 1 ; j <= i-1 ; j ++ ) 18 // j + (i-j) 19 memo[i] = max3(j*(i-j) , j*memo[i-j] , memo[i] ); 20 21 return memo[n]; 22 } 23 };
相关
标签:closed close color assert code cto open mamicode private
原文地址:https://www.cnblogs.com/cxc1357/p/12717372.html