Easy DP solution


  • 0
    V
    public int integerBreak(int n) {
            HashMap<Integer, Integer> dp = new HashMap<>();
            dp.put(1, 1);
            int current = 0, max = 0;
            for(int i = 2; i <= n; i++){
                max = 0;
                for(int j=1; j <= i/2; j++){
                    current = Math.max(j, dp.get(j)) * Math.max(i-j, dp.get(i-j));
                    max = Math.max(max, current);
                }
                dp.put(i, max);
            }
            return dp.get(n);
        }

Log in to reply
 

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