题解 | #小猪摘水果#
小猪摘水果
https://www.nowcoder.com/practice/fdb76b9170dc4e689a7eceee97159d96
考察知识点:数组、前缀和、差分数组
题目分析:
题目给出的序列是一个差分数组。对差分数组求前缀和就能得到原序列。然后维护一个最大值即可。
所用编程语言:C++
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param fruit int整型vector
* @return int整型
*/
int mostFruitTree(vector<int>& fruit) {
// write code here
int size = fruit.size();
int value = 10;
int maxFruit = 10;
for (auto &num: fruit) {
value += num;
maxFruit = max(maxFruit, value);
}
return maxFruit;
}
};


美的集团公司福利 767人发布