尺取法 - L.J.SHOU的专栏 - 博客频道 - CSDN.NET
方法的思想
The idea is to check elements in a way that's reminiscent of movements of a caterpillar.
The caterpillar crawls through the array. We remember the front and back positions of the
caterpillar, and at every step either of them is moved forward.
分析
基本思想就是让 catepillar 表示 和不大于 s 的连续子数组
Each position of the caterpillar will represent a different contiguous subsequence in which
the total of the elements is not greater than s. Let's initially set the caterpillar on the first
element. Next we will perform the following steps:
- if we can, we move the right end (front) forward and increase the size of the caterpillar;
- otherwise, we move the left end (back) forward and decrease the size of the caterpillar.
In this way, for every position of the left end we know the longest caterpillar that covers
elements whose total is not greater than s. If there is a subsequence whose total of elements
equals s, then there certainly is a moment when the caterpillar covers all its elements.
Read full article from 尺取法 - L.J.SHOU的专栏 - 博客频道 - CSDN.NET
No comments:
Post a Comment