作者:小迅
链接:https://leetcode.cn/problems/max-value-of-equation/solutions/2352735/dan-diao-zhan-zhu-shi-chao-ji-xiang-xi-b-fnzc/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
题目
示例
思路
给定一个二维数组,按要求返回最大值
代码
int findMaxValueOfEquation(int** points, int pointsSize, int* pointsColSize, int k){
int stack[pointsSize][2];
int first = 0, end = 0;
int max = INT_MIN;//初始化
for (int i = 0; i < pointsSize; ++i) {//枚举元素
int x = points[i][0], y = points[i][1];
while ((first != end) && (stack[first][0] < x - k)) {
++first;//x下标不满足k限制,x单调递增,后续x肯定也用不上
}
//有元素存在,从最前取最大值
if (first != end) //保存最大值
max = fmax(max, x+y+stack[first][1]);
while ((first != end) && (stack[end-1][1] <= y - x)) {
--end;//只要最大值
}
stack[end][0] = x;
stack[end++][1] = y-x;//入栈
}
return max;
}
作者:小迅
链接:https://leetcode.cn/problems/max-value-of-equation/solutions/2352735/dan-diao-zhan-zhu-shi-chao-ji-xiang-xi-b-fnzc/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。