求文两篇,给包子# Biology - 生物学
H*e
1 楼
他给的算法是:
maxsofar = 0;
maxendinghere = 0;
for i = 0: n-1
maxendinghere = max(maxendinghere + x[i], 0)
maxsofar = max(maxsofar, maxendinghere)
end
这个如果全部都是负数(-5 -1 -2 -4 -3),算出来的是0,which is not true; 应该是
-1
我觉得应该改下:
maxsofar = -INF;
maxendinghere = -INF;
for i = 0: n-1
maxendinghere = max(maxendinghere + x[i], x[i])
maxsofar = max(maxsofar, maxendinghere)
end
am i missing something?
谢谢
maxsofar = 0;
maxendinghere = 0;
for i = 0: n-1
maxendinghere = max(maxendinghere + x[i], 0)
maxsofar = max(maxsofar, maxendinghere)
end
这个如果全部都是负数(-5 -1 -2 -4 -3),算出来的是0,which is not true; 应该是
-1
我觉得应该改下:
maxsofar = -INF;
maxendinghere = -INF;
for i = 0: n-1
maxendinghere = max(maxendinghere + x[i], x[i])
maxsofar = max(maxsofar, maxendinghere)
end
am i missing something?
谢谢