Java solution with O(n) complexity


  • 0
    S
    public class Solution {
        public int integerBreak(int num) {
            int result =1;
            if(num == 1 || num ==2)
            return 1;
            if(num ==3)
            return 2;
            while(num > 4){
            result = result * 3;
            num = num - 3;
        }
        result = result * num;
        return result;
        }
    }

  • 0
    X

    In fact you don't need to consider when num equals to 1, or simply all numbers which less than 2 are just invalid input here.


Log in to reply
 

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