Subarray 问题

Subarray 问题常用思路

1. 从第i个到第j个 subarray 之和 转换成 sum(0,,j) - sum(0,,i)

使用典型题目:sum divide by k, sum equal to k,sum multiple of k

 

2. maximum subarray sum / product 

单一subarray问题,可以思考当1 pass到第i个的时候和已知包含(i-1)个关系,同时维持一个全局变量来记录所有已知的最优解

product稍微复杂一点,需要考虑0 reset 的问题

max_so_far is updated by taking the maximum value among:

  1. Current number.
    • This value will be picked if the accumulated product has been really bad (even compared to the current number). This can happen when the current number has a preceding zero (e.g. [0,4]) or is preceded by a single negative number (e.g. [-3,5]).
  2. Product of last max_so_far and current number.
    • This value will be picked if the accumulated product has been steadily increasing (all positive numbers).
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值