题解 | #牛群售价预测#
牛群售价预测
https://www.nowcoder.com/practice/bbdb8d6f3a2e434e87f749358d16d653
题目考察的知识点:贪心
题目解答方法的文字分析:遍历数组,算出利润,找出最大利润
本题解析所用的编程语言:c++
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param prices int整型vector
* @return int整型
*/
int max_profit(vector<int>& prices) {
// write code here
int profit = 0;
for (int i = 0; i < prices.size(); ++i)
{
for (int j = i + 1; j < prices.size(); ++j)
{
int px = prices[j] - prices[i];
profit = max(profit, px);
}
}
return profit;
}
};