Skip to content

Commit 204a45c

Browse files
Added new Integer Break Problem (#27)
Co-authored-by: Gourav Rusiya <[email protected]>
1 parent 7591d40 commit 204a45c

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

C++/Integer-Break.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
*
3+
*
4+
* Runtime : 4 ms
5+
* Memory : 6 MB
6+
*
7+
*
8+
*/
9+
class Solution {
10+
public:
11+
12+
int integerBreak(int n) {
13+
14+
// when n = 0,1 no possible cuts hence store 0
15+
int val[n + 1];
16+
val[0] = val[1] = 0;
17+
18+
// calculate for all possible cuts possible from 1 to n
19+
for (int i = 1; i <= n; i++) {
20+
int max_value = INT_MIN;
21+
22+
/*for n = 6 calculate for all possible cuts like maximum of
23+
{(1,5),(5,1)}, {(4,2),(2,4)},{ (3,3) }
24+
*/
25+
for (int j = 1; j <= i / 2; j++) {
26+
max_value = max(max_value, max((i - j) * j, j * val[i - j]));
27+
}
28+
29+
val[i] = max_value;
30+
}
31+
32+
return val[n];
33+
34+
}
35+
};

README.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,12 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
214214

215215
## Dynamic Programming
216216

217-
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
218-
| --- | ---------------------------------------------------------------------------------------- | ------------------------------------------- | -------- | -------- | ---------- | --- | ---- |
219-
| 416 | [ Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/) | [C++](./C++/Partition-Equal-Subset-Sum.cpp) | _O(n^2)_ | _O(n^2)_ | Medium | DP | |
220-
| 56 | [Wildcard Matching](https://leetcode.com/problems/wildcard-matching/) | [Python](./Python/wildcard-matching.py) | _O(n^2)_ | _O(n^2)_ | Hard | DP | |
221-
| 139 | [Word Break](https://leetcode.com/problems/word-break/) | [Python](./Python/word-break-1.py) | _O(n^3)_ | _O(n)_ | Medium | DP | |
217+
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
218+
| --- | --------------------------------------------------------------------- | ----------------------------------------- | ------ | ------ | ---------- | ----- | ---- |
219+
| 416 | [ Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/)| [C++](./C++/Partition-Equal-Subset-Sum.cpp)| _O(n^2)_ | _O(n^2)_ | Medium | DP | |
220+
| 56 | [Wildcard Matching](https://leetcode.com/problems/wildcard-matching/) | [Python](./Python/wildcard-matching.py) | _O(n^2)_ | _O(n^2)_ | Hard | |
221+
| 343 | [Integer Break](https://leetcode.com/problems/integer-break/) | [C++](./C++/Integer-Break.cpp) | _O(n^2)_ | _O(n)_ | Medium | |
222+
| 139 | [Word Break](https://leetcode.com/problems/word-break/) | [Python](./Python/word-break-1.py) | _O(n^3)_ | _O(n)_ | Medium | DP | |
222223

223224

224225
<br/>

0 commit comments

Comments
 (0)