We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0ac8f44 commit 40e7a58Copy full SHA for 40e7a58
kotlin/0343-integer-break.kt
@@ -1,5 +1,5 @@
1
/*
2
-* DP solution
+* DP solution O(n^2) time and space
3
*/
4
class Solution {
5
fun integerBreak(n: Int): Int {
@@ -19,7 +19,7 @@ class Solution {
19
}
20
21
22
-* DFS + memoization solution
+* DFS + memoization solution O(n^2) time and space
23
24
25
@@ -40,3 +40,20 @@ class Solution {
40
return dfs(n)
41
42
43
+
44
+// Math solution O(n) time and O(1) space
45
+class Solution {
46
+ fun integerBreak(n: Int): Int {
47
+ if (n < 4) return n - 1
48
49
+ var res = 1
50
+ var n2 = n
51
+ while (n2 > 4) {
52
+ res *= 3
53
+ n2 -=3
54
+ }
55
56
+ res *= n2
57
+ return res
58
59
+}
0 commit comments