File tree Expand file tree Collapse file tree 5 files changed +82
-1
lines changed Expand file tree Collapse file tree 5 files changed +82
-1
lines changed Original file line number Diff line number Diff line change @@ -3,5 +3,5 @@ project(cpp_0343)
3
3
4
4
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )
5
5
6
- set (SOURCE_FILES main .cpp )
6
+ set (SOURCE_FILES main3 .cpp )
7
7
add_executable (cpp_0343 ${SOURCE_FILES} )
Original file line number Diff line number Diff line change @@ -44,6 +44,7 @@ class Solution {
44
44
}
45
45
};
46
46
47
+
47
48
int main () {
48
49
49
50
cout << Solution ().integerBreak (2 ) << endl;
Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ class Solution {
33
33
}
34
34
};
35
35
36
+
36
37
int main () {
37
38
38
39
cout << Solution ().integerBreak (2 ) << endl;
Original file line number Diff line number Diff line change
1
+ // / Source : https://leetcode.com/problems/integer-break/description/
2
+ // / Author : liuyubobobo
3
+ // / Time : 2019-07-24
4
+
5
+ #include < iostream>
6
+ #include < vector>
7
+
8
+ using namespace std ;
9
+
10
+ // / Dynamic Programming
11
+ // / Deal with 2, 3 and 4 seperately
12
+ // / Time Complexity: O(n^2)
13
+ // / Space Complexity: O(n)
14
+ class Solution {
15
+
16
+ public:
17
+ int integerBreak (int n) {
18
+
19
+ assert (n >= 1 );
20
+ if (n == 2 ) return 1 ;
21
+ if (n == 3 ) return 2 ;
22
+
23
+ vector<int > memo (n + 1 , -1 );
24
+ memo[0 ] = 0 ;
25
+ memo[1 ] = 1 ;
26
+ memo[2 ] = 2 ;
27
+ memo[3 ] = 3 ;
28
+ for (int i = 2 ; i <= n ; i ++)
29
+ for (int j = 1 ; j <= i / 2 ; j ++)
30
+ memo[i] = max (memo[i], memo[j] * memo[i - j]);
31
+
32
+ return memo[n];
33
+ }
34
+ };
35
+
36
+
37
+ int main () {
38
+
39
+ cout << Solution ().integerBreak (2 ) << endl;
40
+ cout << Solution ().integerBreak (3 ) << endl;
41
+ cout << Solution ().integerBreak (4 ) << endl;
42
+ cout << Solution ().integerBreak (10 ) << endl;
43
+
44
+ return 0 ;
45
+ }
Original file line number Diff line number Diff line change
1
+ /// Source : https://leetcode.com/problems/integer-break/description/
2
+ /// Author : liuyubobobo
3
+ /// Time : 2019-07-24
4
+
5
+ /// Dynamic Programming
6
+ /// Deal with 2, 3 and 4 seperately
7
+ /// Time Complexity: O(n^2)
8
+ /// Space Complexity: O(n)
9
+ public class Solution3 {
10
+
11
+ public int integerBreak (int n ) {
12
+
13
+ if (n < 1 )
14
+ throw new IllegalArgumentException ("n should be greater than zero" );
15
+ if (n == 2 ) return 1 ;
16
+ if (n == 3 ) return 2 ;
17
+
18
+ int [] memo = new int [n +1 ];
19
+ memo [1 ] = 1 ;
20
+ memo [2 ] = 2 ;
21
+ memo [3 ] = 3 ;
22
+ for (int i = 2 ; i <= n ; i ++)
23
+ for (int j = 1 ; j <= i / 2 ; j ++)
24
+ memo [i ] = Math .max (memo [i ], memo [j ] * memo [i - j ]);
25
+
26
+ return memo [n ];
27
+ }
28
+
29
+ public static void main (String [] args ) {
30
+
31
+ System .out .println ((new Solution2 ()).integerBreak (2 ));
32
+ System .out .println ((new Solution2 ()).integerBreak (10 ));
33
+ }
34
+ }
You can’t perform that action at this time.
0 commit comments