Open youngyangyang04 opened 3 months ago
太巧妙了!
for (int i = 1; i < n; i++) {
if (ratings[i] > ratings[i - 1]) {
candy[i] = Math.max(candy[i], candy[i - 1] + 1);
}
}
for (int i = n - 2; i >= 0; i--) {
if (ratings[i] > ratings[i + 1]) {
candy[i] = Math.max(candy[i], candy[i + 1] + 1);
}
}
// 例: ratings = [1,3,4,5,2], candy = [1, 1, 1, 1, 1]
第一轮 for 循环后: candy = [1, 2, 3, 4, 1]
第二轮 for 循环后: candy = [1, 2, 3, 4, 1]
https://www.programmercarl.com/0135.%E5%88%86%E5%8F%91%E7%B3%96%E6%9E%9C.html