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 | public class HelloWorld{ public static void main(String []args){ System.out.println(integerBreak(11)); } // For any n greater than 4, so 5 or up, will always see a // repeating 3 as the pattern. With each 3, we subtract 3 // from the original n and eventually we will get a number // that is <= 4 and that will be the last number in the multiplication. public static int integerBreak(int n) { if (n == 2) return 1; if (n == 3) return 2; int result = 1; while (n > 4) { result = result * 3; n = n - 3; } result = result * n; return result; } } |
Wednesday, 22 June 2016
13. [Technical Interview] Integer Break
You will need to spot the repeating 3 pattern. Followed this link to understand the problem: https://github.com/haoel/leetcode/blob/master/algorithms/cpp/integerBreak/IntegerBreak.cpp
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment