解题思路:就是将每天的利润都算出来,只要是正的就加加
int maxProfit(int* prices, int pricesSize){
int result = 0;
int i;
for(i = 1; i < pricesSize; ++i) {
if(prices[i] > prices[i-1])
result+= prices[i]-prices[i-1];
}
return result;
}
#define max(a,b) ((a > b) ? a : b)
bool canJump(int* nums, int numsSize) {
int cover = 0;
int i;
for(i = 0; i <= cover; ++i){
cover = max(i + nums[i], cover);
if(cover >= numsSize-1)
return true;
}
return false;
}
这道题和跳跃1相类似,也是覆盖范围来决定的,当覆盖还不到最后的时候,步数加加
#define max(a,b) (a > b ? a : b)
int jump(int* nums, int numsSize) {
if(numsSize == 1)
return 0;
int cur = 0;
int ans = 0;
int next = 0;
for(int i = 0; i < numsSize; i++){
next = max(i + nums[i], next);
if(i == cur){
ans++;
cur = next;
if(next >= numsSize - 1)
break;
}
}
return ans;
}