题解 | #牛群放牧顺序#
牛群放牧顺序
https://www.nowcoder.com/practice/69f5f2d04d1c41df8d4e0691f6ef6935
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param ratings int整型vector * @return int整型 */ int min_pasture_time(vector<int>& ratings) { // write code here int n = ratings.size(); vector<int> res(n, 1); for (int i = 1; i < n; ++i) if (ratings[i] > ratings[i - 1]) res[i] = res[i - 1] + 1; int sum = res[n - 1]; for (int i = n - 2; i >= 0; --i) { if (ratings[i] > ratings[i + 1] && res[i] <= res[i + 1]) res[i] = res[i + 1] + 1; sum += res[i]; } return sum; } };