Tuesday, January 27, 2015

[LintCode]Minimum Subarray



max subarray是类似的题目,divide and conquer的方法是类似的,我们写一下DP的方法,我们只需要稍微改变一下DP方程就可以了:
  • localMin[i] = min(localMin[i - 1] + A[i], A[i]);
  • globalMin[i] = min(globalMin[i - 1], localMin[i]);
时间复杂度O(n), 空间复杂度可以优化到O(1),代码如下:

No comments:

Post a Comment