Mathematical solution using optimization in Java


  • 0
    D
    class Solution
    {
        public int integerBreak(int n)
        {
        	int prod = 1;
        	if(n==2) return 1;
        	if(n==3) return 2;
        	
        	switch(n%3)
        	{
        	case 0:
        		prod = (int)Math.pow(3,n/3);
        		break;
        	case 1:
        		prod = 2 * 2 * (int)Math.pow(3, (n-4)/3);
        		break;
        	case 2:
        		prod = 2 * (int)Math.pow(3, (n-2)/3);
        		break;
        	}
        	return prod; 
        }
    }
    

Log in to reply
 

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