forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegerBreak.swift
More file actions
30 lines (26 loc) · 775 Bytes
/
IntegerBreak.swift
File metadata and controls
30 lines (26 loc) · 775 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* Question Link: https://leetcode.com/problems/integer-break/
* Primary idea: Final Result must be split as 2^m * 3^n. Lets say p = p1 + p2 +...+ pn,
* if p1 could be split as 2(p1 - 2), than it would be greater than p1 if p1 > 4.
* same thing for 3(p1 - 3). Thus we spilt the original number to multiple 3s and 2s to
* get the final result
* Time Complexity: O(logn), Space Complexity: O(1)
*/
class IntegerBreak {
func integerBreak(n: Int) -> Int {
var num = n
var res = 1
if num == 2 {
return 1
}
if num == 3 {
return 2
}
while num > 4 {
res *= 3
num -= 3
}
res *= num
return res
}
}