189.轮转数组
给定一个整数数组 nums
,将数组中的元素向右轮转 k
个位置,其中 k
是非负数。
方法一:用取模的形式放到新数组中
class Solution {
public void rotate(int[] nums, int k) {
int n = nums.length;
int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[(i+k)%n]=nums[i];//%取mod的符号只与被模的数一致。比如数组长10,那么1%10=11%10
}
for(int i=0;i<n;i++){
nums[i]=arr[i];
}
}
}
121.买卖股票的最佳时机
给定一个数组 prices
,它的第 i
个元素 prices[i]
表示一支给定股票第 i
天的价格。
你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。
返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0
。
class Solution {
public int maxProfit(int[] prices) {
//买卖股票算利润,买入前的时间点是不能卖出的,只能用后面的减去前面买入的。
int maxProfit = 0;
Integer lowerPrice = null;
for(int i=0;i<prices.length;i++){
if(lowerPrice == null){
lowerPrice = prices[i];
}
if(prices[i]<lowerPrice){
lowerPrice = prices[i];
}else if(prices[i]-lowerPrice>maxProfit){
maxProfit = prices[i]-lowerPrice;
}
}
return maxProfit;
}
}
/*
Integer.MAX_VALUE表示int数据类型的最大取值数:2 147 483 647
Integer.MIN_VALUE表示int数据类型的最小取值数:-2 147 483 648
*/
122.买卖股票的最佳时机II
给你一个整数数组 prices
,其中 prices[i]
表示某支股票第 i
天的价格。
在每一天,你可以决定是否购买和/或出售股票。你在任何时候 最多 只能持有 一股 股票。你也可以先购买,然后在 同一天 出售。
返回 你能获得的 最大 利润 。
方法一:记忆化搜索
递归会不断递归下去直到穷举所有可能;
class Solution {
public int maxProfit(int[] prices) {
Integer[][] ans = new Integer[prices.length+1][2];
return calculateMaxProfit(prices, 0, 0,ans);
}
private int calculateMaxProfit(int[] prices, int day, int state,Integer[][] ans) {
if(ans[day][state]!=null){
return ans[day][state];
}
if (day == prices.length) {
return 0;
}
int maxProfitNotHolding = 0;
int maxProfitHolding = 0;
//同一天一个股票只有1.买入或者卖出2.保持不变
maxProfitNotHolding = calculateMaxProfit(prices, day + 1, state,ans);// 当日既不买入股票也不卖出股票;
//当state == 0的时候只能买入股票
if (state == 0) {
maxProfitHolding = calculateMaxProfit(prices, day + 1, 1,ans) - prices[day];//买入股票
} else {
maxProfitHolding = calculateMaxProfit(prices, day + 1, 0,ans) + prices[day];//卖出股票
}
ans[day][state] = maxProfitHolding > maxProfitNotHolding ? maxProfitHolding : maxProfitNotHolding;
return ans[day][state];
}
}
方法二:动态规划DP
class Solution {
public int maxProfit(int[] prices) {
int[][] dp = new int[prices.length][2];
dp[0][0] = 0;
dp[0][1] = -prices[0];//初始化 所有i应该从1开始 因为没有dp[-1]的初始化值
for (int i = 1; i < prices.length; i++) {
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
}
return dp[prices.length - 1][0];
}
}
空间优化:因为只需要知道前一个的两种状态就可以得出当前的值。所以只要存这两个值就行
注意到上面的状态转移方程中,每一天的状态只与前一天的状态有关,而与更早的状态都无关,因此我们不必存储这些无关的状态,只需要将 dp[i−1][0]\textit{dp}[i-1][0]dp[i−1][0] 和 dp[i−1][1]\textit{dp}[i-1][1]dp[i−1][1] 存放在两个变量中,通过它们计算出 dp[i][0]\textit{dp}[i][0]dp[i][0] 和 dp[i][1]\textit{dp}[i][1]dp[i][1] 并存回对应的变量,以便于第 i+1i+1i+1 天的状态转移即可。
方法三:贪心
由于股票购买没有限制,把所有价格上涨的区间累计 就是最优利益
class Solution {
public int maxProfit(int[] prices) {
int ans = 0;
for(int i=1;i<prices.length;i++){
ans+=Math.max(0,prices[i]-prices[i-1]);
}
return ans;
}
}
这段代码采用了贪心算法的思想,其原理是在每一步都做出当前看起来最优的选择,从而期望最终达到全局最优解。在这个问题中,贪心算法的思想是尽可能地多买卖股票,只要能够获得利润就进行交易。
具体来说,贪心算法的实现如下:
- 遍历股票价格数组,从第二天开始(i=1)。
- 对于每一天,比较当前股价与前一天的股价,如果当前股价高于前一天,则认为可以在前一天买入,当前天卖出,从而获得利润。
- 利润通过
prices[i] - prices[i - 1]
计算,如果是正数,则表示有利润,加到最终结果ans
上;如果是负数或零,则表示无法获得利润,忽略这次交易。 - 最终,
ans
即为贪心策略下的最大利润。
这种贪心算法的思想是尽可能地捕捉到股票价格的上涨趋势,每次遇到上涨就进行买卖,从而累积利润。这样做的原理是相信在上涨趋势中,频繁买卖可以获得更大的利润。贪心算法不保证一定能得到全局最优解,但在某些问题中,它能够提供较好的解决方案,并且具有较低的时间复杂度。
55.跳跃游戏
给你一个非负整数数组 nums
,你最初位于数组的 第一个下标 。数组中的每个元素代表你在该位置可以跳跃的最大长度。
判断你是否能够到达最后一个下标,如果可以,返回 true
;否则,返回 false
。
方法一:dp动态规划
class Solution {
public boolean canJump(int[] nums) {
boolean[] dp = new boolean[nums.length];
dp[0] = true;
for (int i = 1; i < nums.length; i++) {
for (int j = 0; j < i; j++) {
if (dp[j] && (j + nums[j] >= i)) {
dp[i] = true;
break;
}
}
}
return dp[nums.length - 1];
}
}