/// 343. Integer Break /// https://leetcode.com/problems/integer-break/description/ /// 暴力搜索 /// 在Leetcode中提交这个版本的代码会超时! (Time Limit Exceeded) /// 时间复杂度: O(n^n) /// 空间复杂度: O(n) public class Solution1 { public int integerBreak(int n) { if(n < 1) throw new IllegalArgumentException("n should be greater than zero"); return breakInteger(n); } // 将n进行分割(至少分割两部分), 可以获得的最大乘积 private int breakInteger(int n){ if(n == 1) return 1; int res = -1; for(int i = 1 ; i <= n - 1 ; i ++) res = max3(res, i * (n - i), i * breakInteger(n - i)); return res; } private int max3(int a, int b, int c){ return Math.max(a, Math.max(b, c)); } public static void main(String[] args) { System.out.println((new Solution1()).integerBreak(2)); System.out.println((new Solution1()).integerBreak(10)); } }