CLRS 4.1 EXERCISES

4.1-4

Suppose we change the definition of the maximum subarray problem to allow the result to be an empty subarray, where the sum of the values of an empty subarray is 0. How would you change any of the algorithms that do not allow empty subarrays to permit an empty subarray to be the result.


    FIND-MAXIMUM-SUBARRAY 最后一部分的伪代码是

if left-sum >= right-sum && left-sum >= cross-sum
    return (left-low, left-high, left-sum)
if right-sum >= left-sum && right-sum >= cross-sum
    return (right-low, right-high, right-sum)
else 
    return (cross-low, cross-high, cross-sum

    把伪代码改成

if left-sum >= right-sum && left-sum >= cross-sum && left-sum > 0
    return (left-low, left-high, left-sum)
if right-sum >= left-sum && right-sum >= cross-sum && right-sum > 0
    return (right-low, right-high, right-sum)
if cross-sum >= left-sum && cross-sum >= right-sum && cross-sum > 0
    return (cross-low, cross-high, cross-sum)
else 
    return (0, 0, 0)


4.1-5

Use the following ideas to develop a nonrecursive, linear-time algorithm for the maximum-subarray problem. Start at the left end of the array, and progress toward the right, keeping track of the maximum subarray seen so far. Knowing a maximum subarray of A[1..j], extend the answer to find a maximum subarray ending at index j+1 by using the following observation: a maximum subarray of A[1..j+1] is either a maximum subarray of A[1..j] or a subarray A[i..j+1], for some 1 <= i <= j + 1. Determine a maximum subarray of the form A[i..j+1] in constant time based on knowing a maximum subarray ending at index j.

代码链接

转载于:https://my.oschina.net/jerryWu/blog/197954

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值