Skip to content

Commit a1113e2

Browse files
authored
Update 0343-integer-break.kt
1 parent 40e7a58 commit a1113e2

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

kotlin/0343-integer-break.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,22 @@ class Solution {
5757
return res
5858
}
5959
}
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

Comments
 (0)