乘积最大子数组
解法一
class Solution { public int maxProduct(int[] nums) { int length = nums.length; int[] maxF = new int[length]; int[] minF = new int[length]; System.arraycopy(nums, 0, maxF, 0, length); System.arraycopy(nums, 0, minF, 0, length); for (int i = 1; i < length; ++i) { maxF[i] = Math.max(maxF[i - 1] * nums[i], Math.max(nums[i], minF[i - 1] * nums[i])); minF[i] = Math.min(minF[i - 1] * nums[i], Math.min(nums[i], maxF[i - 1] * nums[i])); } int ans = maxF[0]; for (int i = 1; i < length; ++i) { ans = Math.max(ans, maxF[i]); } return ans; } }
零钱兑换
public class Solution { public int coinChange(int[] coins, int amount) { int max = amount + 1; int[] dp = new int[amount + 1]; Arrays.fill(dp, max); dp[0] = 0; for (int i = 1; i <= amount; i++) { for (int j = 0; j < coins.length; j++) { if (coins[j] <= i) { dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1); } } } return dp[amount] > amount ? -1 : dp[amount]; } }
戳气球
class Solution { public int maxCoins(int[] nums) { int n = nums.length; int[][] rec = new int[n + 2][n + 2]; int[] val = new int[n + 2]; val[0] = val[n + 1] = 1; for (int i = 1; i <= n; i++) { val[i] = nums[i - 1]; } for (int i = n - 1; i >= 0; i--) { for (int j = i + 2; j <= n + 1; j++) { for (int k = i + 1; k < j; k++) { int sum = val[i] * val[k] * val[j]; sum += rec[i][k] + rec[k][j]; rec[i][j] = Math.max(rec[i][j], sum); } } } return rec[0][n + 1]; } }