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 40e7a58 commit a1113e2Copy full SHA for a1113e2
kotlin/0343-integer-break.kt
@@ -57,3 +57,22 @@ class Solution {
57
return res
58
}
59
60
+
61
+// Mathimatically solved O(1)
62
+class Solution {
63
+ fun integerBreak(n: Int): Int {
64
+ if (n < 4) return n - 1
65
66
+ var res = n / 3
67
+ var rem = n % 3
68
69
+ if (rem == 1) {
70
+ rem = 4
71
+ res--
72
+ } else if (rem == 0) {
73
+ rem = 1
74
+ }
75
76
+ return (Math.pow(3.toDouble(), res.toDouble()) * rem).toInt()
77
78
+}
0 commit comments