题解 | #牛群买卖策略优化#
牛群买卖策略优化
https://www.nowcoder.com/practice/c8514318443a48218efde630ae11b4c3
题目考察的知识点:贪心
题目解答方法的文字分析:只要利润差大于0,那么就可以进行买卖。
本题解析所用的编程语言:c++
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param prices int整型vector
* @return int整型
*/
int max_profitv2(vector<int>& prices) {
// write code here
int profit = 0;
for (int i = 1; i < prices.size(); ++i)
{
int x = prices[i] - prices[i - 1];
if (x > 0)
profit += x;
}
return profit;
}
};
查看6道真题和解析
