my O(1) complexity solution

  • 1

    we can see that exclude 2 and 3,other numbers just compute how many "3" (n-2) have,the reason we need to -2 is that 4(2×2) is greater than 3(1×2).

    public class Solution {
        public int integerBreak(int n) {
            if (n==2) return 1;
            if (n==3) return 2;
            int num_of_3 = (n-2)/3;
            int pow_of_3 = (int) Math.pow(3,num_of_3);
            return pow_of_3*(n-3*num_of_3);   

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.