此文将持续更新,涉及以下题目:
买卖股票的最佳时机
买卖股票的最佳时机 II
买卖股票的最佳时机 III
买卖股票的最佳时机 IV
最佳买卖股票时机含冷冻期
买卖股票的最佳时机含手续费
难度简单
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
注意:给定 n 是一个正整数。
示例 1:
输入: 2
输出: 2
解释: 有两种方法可以爬到楼顶。
1. 1 阶 + 1 阶
2. 2 阶
示例 2:
输入: 3
输出: 3
解释: 有三种方法可以爬到楼顶。
1. 1 阶 + 1 阶 + 1 阶
2. 1 阶 + 2 阶
3. 2 阶 + 1 阶
class Solution {
public int climbStairs(int n) {
int[] dp = new int[n + 1];
if(n==1) return 1;
if(n==2) return 2;
dp[1] = 1;
dp[2] = 2;
if(n>=3){
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
}
return dp[n];
}
}
难度简单
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
如果你最多只允许完成一笔交易(即买入和卖出一支股票一次),设计一个算法来计算你所能获取的最大利润。
注意:你不能在买入股票前卖出股票。
示例 1:
输入: [7,1,5,3,6,4]
输出: 5
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。
示例 2:
输入: [7,6,4,3,1]
输出: 0
解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
// 暴力求解
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int maxProfit(int prices[]) {
int maxprofit = 0;
for (int i = 0; i < prices.length - 1; i++) {
for (int j = i + 1; j < prices.length; j++) {
int profit = prices[j] - prices[i];
if (profit > maxprofit)
maxprofit = profit;
}
}
return maxprofit;
}
}
//leetcode submit region end(Prohibit modification and deletion)
// 动态规划求解,待优化,超出内存限制
class Solution {
public int maxProfit(int[] prices) {
int[][] dp = new int[prices.length][prices.length];
for (int i = 0; i < prices.length; i++) {
dp[i][i] = 0;
}
int result = 0;
for (int i = 0; i < prices.length; i++) {
for (int j = 0; j < i; j++) {
dp[i][j] = prices[i] - prices[j];
if (dp[i][j] > result) {
result = dp[i][j];
}
}
}
return result;
}
}
通过不断跟最低值比较,得到当前最低的利润。
当前时间减掉最低的价格,不断跟maxprofit 进行比较,得到maxprofit。
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int maxProfit(int prices[]) {
if (prices.length == 0) {
return 0;
}
int maxprofit = 0;
int minprice = prices[0];
for (int i = 0; i < prices.length; i++) {
minprice = Math.min(minprice, prices[i]);
maxprofit = Math.max(maxprofit, prices[i] - minprice);
}
return maxprofit;
}
}
//leetcode submit region end(Prohibit modification and deletion)
难度中等
给定不同面额的硬币 coins 和一个总金额 amount。编写一个函数来计算可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1。
示例 1:
输入: coins = [1, 2, 5], amount = 11输出: 3
解释: 11 = 5 + 5 + 1
示例 2:
输入: coins = [2], amount = 3输出: -1
import java.util.Arrays;
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int coinChange(int[] coins, int amount) {
if (amount == 0) {
return 0;
}
int[] dp = new int[amount + 1];
Arrays.fill(dp, amount + 1);
dp[0] = 0;
for (int coin : coins) {
for (int i = coin; i < amount + 1; i++) {
dp[i] = Math.min(dp[i - coin] + 1, dp[i]);
}
}
return dp[amount] == amount + 1 ? -1 : dp[amount];
}
}
//leetcode submit region end(Prohibit modification and deletion)
难度中等
给定不同面额的硬币和一个总金额。写出函数来计算可以凑成总金额的硬币组合数。假设每一种面额的硬币有无限个。
示例 1:
输入: amount = 5, coins = [1, 2, 5]
输出: 4
解释: 有四种方式可以凑成总金额:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
示例 2:
输入: amount = 3, coins = [2]
输出: 0
解释: 只用面额2的硬币不能凑成总金额3。
示例 3:
输入: amount = 10, coins = [10]
输出: 1
注意:
你可以假设:
-
0 <= amount (总金额) <= 5000
-
1 <= coin (硬币面额) <= 5000
-
硬币种类不超过 500 种
-
结果符合 32 位符号整数
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int change(int amount, int[] coins) {
int[] dp = new int[amount + 1];
dp[0] = 1;
for (int coin : coins) {
for (int i = coin; i < amount + 1; i++) {
dp[i] += dp[i - coin];
}
}
return dp[amount];
}
}
//leetcode submit region end(Prohibit modification and deletion)