Skip to content

Commit 10890c4

Browse files
committed
0343 new algo added.
1 parent 371d7a3 commit 10890c4

File tree

5 files changed

+82
-1
lines changed

5 files changed

+82
-1
lines changed

0343-Integer-Break/cpp-0343/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ project(cpp_0343)
33

44
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
55

6-
set(SOURCE_FILES main.cpp)
6+
set(SOURCE_FILES main3.cpp)
77
add_executable(cpp_0343 ${SOURCE_FILES})

0343-Integer-Break/cpp-0343/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class Solution {
4444
}
4545
};
4646

47+
4748
int main() {
4849

4950
cout << Solution().integerBreak(2) << endl;

0343-Integer-Break/cpp-0343/main2.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Solution {
3333
}
3434
};
3535

36+
3637
int main() {
3738

3839
cout << Solution().integerBreak(2) << endl;

0343-Integer-Break/cpp-0343/main3.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}

0 commit comments

Comments
 (0)