Subarray 问题

本文介绍了Subarray问题的常见解题思路,包括转化求和、最大子数组和/积、变种问题及最短子数组的双指针方法。特别讨论了如何利用DP解决重复子数组的最大长度问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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).
    • <
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值