O(n) time complexity, O(1) space Java solution


  • 1
    K

    We want to break the given number into as many "3" as possible, until the n shrink to 4. After breaking, we should only have 3s and 2s.

    public class Solution {

    public int integerBreak(int n) {
        if (n == 2 || n == 3) {
            return n - 1;
        }
        
        int result = 1;
        while (n > 4) {
            result *= 3;
            n -= 3;
        }
        result *= n;
        return result;
    }
    

    }


Log in to reply
 

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