第一题、买卖股票的最佳时机|| 力扣题目链接
思路:总利润,就是保证每天有利润就可以了,然后加起来就是整体利润最大了。
class Solution {
public:
int maxProfit(vector<int>& prices) {
int count = 0;
for(int i = 1; i < prices.size(); i++){
if(prices[i] - prices[i-1] > 0){
count += prices[i] - prices[i-1];
}
}
return count;
}
};
第二题、跳跃游戏 力扣题目链接
class Solution {
public:
bool canJump(vector<int>& nums) {
int index = 0;
if (nums.size() == 1) return true;
for(int i = 0; i <= index; i++){
index = max(i + nums[i], index);
if(index >= nums.size()-1){
return true;
}
}
return false;
}
};
第san题、跳跃游戏|| 力扣题目链接
class Solution {
public:
int jump(vector<int>& nums) {
if(nums.size() == 1) return 0;
int curDistance = 0;
int ans = 0;
int nextDistance = 0;
for(int i = 0; i < nums.size(); i++){
nextDistance = max(i + nums[i], nextDistance);
if(i == curDistance){
ans++;
curDistance = nextDistance;
if(nextDistance >= nums.size() - 1) break;
}
}
return ans;
}
};