"Power button" on page 416 title: "segmentation and subsets"

Hello everyone, here is the "power button" Video Interpretations 416 title: "segmentation and subsets."

This question gives us an empty array containing only non-positive integers. We asked if the array can be divided into two subsets, such that the elements of these two subsets and equal.

We see Example 1: An array given is [1, 5, 11, 5]that the sum of all the elements of the array is 22the example given is split [1, 5, 5]and [11].

From this example we know:

1, the divided two portions are a subset, are not required to be part of a continuous array;
2, divided stitching together two subsets of the entire array, and each element can only be used once;
3, a separate element can also be called a sub-set, but empty elements and all the elements can not be called a subset, it should be easy to want to understand.

Solve this problem we need some basic knowledge of "dynamic programming" problem, in fact we are just a little analysis of this issue, it is not difficult to see, it is a typical problem of dynamic programming: "knapsack problem" deformation.

It is obvious from the example given in the meaning of the questions and the topic is to us to find a non-empty subset as a split from a given array, and this subset of all elements and half of the remaining it is not selected as an element to the other half divided, and these two divided equally clearly.

In a number of articles selected certain items, each item only once, just to fill these items capacity sum / 2backpack.

The "0-1 knapsack problem" described it this way:

In the Mremoved items in a volume of several pieces Wof backpack, only one of each item, and they have a respective volume value, ask how to choose the most value of the article such that the backpack can be accommodated under.

Here we would like to note are:

1, lenis the length of the array, it can be considered the number of backpack items;
2, sumis the array of all elements, we have to elect a number of elements and is sum / 2then not selected and also elements of the sum / 2, we note that the title given array contains only positive integers, so sumif it is odd, sum / 2is a fraction of the 0.5 band, will not there is a subset of all integers, and it is a decimal, and this is a special determination;

3, based on experience "knapsack problem", our idea is: a an item to try, little by little, considering the size of the expansion to accommodate the volume (this is important), the whole process is like filling in a two-dimensional form.

Therefore, we should change the ideas come from, we need to consider is the length from a limited array numsin, took out part of the element, exactly equal to a calculated and whether a given value of these elements target = sum / 2.

4, according to the general procedure for solving dynamic programming problems.

(1) First set state.

Status Definition: dp[i][j]Consider index [0, i]all integers in this interval, and among them is able to pick out a few numbers so that the sum of these numbers is just an integer j.

Note: This iis a prefix character, although we just write a number, but the meaning it represents is that we consider is the sub-interval [0, i]in all the elements, subscript iand ithe previous element in our consideration of the subscript iafter the elements must not within the scope of our consideration.

For ease of discussion later, I was unified with "taking into account the index iuntil the elements that the existence of the elements of a subset and is j" to express this meaning.

From the definition of this state, we can see that we are not all of a sudden array of all the items are taken into account, nor is it all of a sudden went consider a subset of the sum is equal target = sum / 2, we are from a smaller problem departure, a number a number to experiment, and to expand a little bit of a subset of considerations of size . This is something we just mentioned before, this idea is very important.

Dynamic path planning a classic thinking is this: from a minimum of problems, little by little to solve the larger problem, until the scale of the problem we have to solve, which is bottom-up "dynamic planning" ideas. In the process of solving, the recording process of solving, in solving the larger problems, we are not solved directly, but directly to check Solution smaller issues have been determined, which is a characteristic of dynamic programming: "optimal substructure."

(2) the state transition equation

Thinking the state transition equation, a lot of time discussing the classification is actually doing, "0-1 knapsack problem" category discussed standard is actually very common, is when we attempt to items one by one, currently considered the election and not vote for this article , you can get the state transition equation.

① If not selected as the index ielement, dp[i][j]the value is entirely dependent dp [i - 1] [j ]

dp[i][j] = dp[i - 1][j]

② If the index for the selection iof elements still have to be classified discussion:

If the standard for the next ivalue of the element, exactly equal j, then I can put alone nums[i]as a subset of, directly dp[i][j] = true;
Otherwise, dp[i][j]we should look at, the number of those before it, namely the subscript [0, i - 1]these numbers inside, if you can find and as [j - nums[i]]some of the numbers. The equation is written like this:

dp[i][j] = (nums[i] == j) or dp[i - 1][j - nums[i]] (j - nums[i] > 0)

So we look at the integration of the above two cases:

dp[i][j] = dp[i - 1][j] or (nums[i] == j) or dp[i - 1][j - nums[i]] (j - nums[i] > 0)

Let us explain this equation :( I wish to explain here, it will not be I get confused.)

(3) Consider initialization

dp[0][0] = false

(4) Consider output

dp[len - 1][target]

Let's write the code:

1, first the length of the array is assigned to a variable len, because the subject has been said array is not empty, it is not necessary for the array length is zero to do a special sentence;

2, since we are looking backpack capacity as sum / 2a subset of, and therefore have to calculate the sum, and do a special sentence, if sum / 2is odd, we have just analyzed, and some can not find such a division, the direct return false ;

3, next is our dynamic planning process of filling in a form, to create a length lenand width target + 1of the table, why should we increase 1it, because the capacity of 0a state is what we need to consider.

It is a Boolean array.

4, fill out the first line, that is, only consider that element at index 0, it is clear that it can only fill that capacity for its own backpack, so we can write directly

dp[0][nums[0]] = true;

But written this way, pay attention to nums [0] there may be cross-border, therefore, be able to write on the premise that nums[0] <= target

5. Next, we use two for loops to fill out the form below

(1) because the first element we have considered, therefore, from the first two elements, namely element at index 1 to start thinking about the second-dimensional volume from 0 up to volume target.

(2) The state transition equation

If the current consideration of this number nums [i] is big, more than j, then
dp [i] [j] is at least dp [i - 1] [j ] values

If nums [i] happens to be j, directly to dp [i] [j] assigned to true, otherwise, the following should be considered in nums [i] j strictly less than the time, nums [i] have selected and not selected In both cases,

If not selected, the direct reference dp [i - 1] Value [j], and
if selected, it should be noted that subscript i - 1 so far, and whether to Couchu j - nums [i] of the subset.

dp[i][j] = dp[i - 1][j] or dp[i - 1][j - nums[i]]

Finally, according to the analysis earlier, two for loop is filled out this form, we return is dp [len - 1] [target].

This is our first version of the code, submitted to the force after deduction of the evaluation system, able to get a pass.

Here we optimize a few details:

1, or due

A logic operation has a characteristic short circuit, and therefore, in fact, found in the process as long as the filling of a cell is true, it is below the grid are all true. In particular, if the value of the last cell in a row is true, the last one of all the lines below that state is necessarily true, the output is certainly true, so after filling out each line, a determination can be done, if The final grid for the true, the entire method can be returned directly.

2, noting that dp[i - 1][j - nums[i]]this expression, although it was established in the case of nums [i] <j but when j == nums [i], we actually discussed, separate nums [i] can constitute and j is a subset of, and therefore will dp[i - 1][0]be set to true is entirely reasonable, please carefully think about, the second dimension is the state 0, its value will only be after the reference value, when nums [i ] == j time, dp [i] [j] reference dp [i - 1] [0 ], it is set to true, though not semantically, but results from the point of view is entirely reasonable.

Write code that can do a little judgment.

We submit a version of the code will be passed.


For the 0-1 knapsack problem experienced friends must know, in fact, to solve this problem, but also in the space to be compressed.

Because we always refer to the current line on its line and its value on the left side of the line, not the value of the reference to the row before, so we are in the process of filling the need to retain only two lines can be, which you can use "scroll array" of coding techniques to achieve. Here it left as an exercise to achieve.

More specifically, we can also state array is set to one line, it's worth the time to update our update from back to front.

So why not update the front backwards it? Still fill out an application before the simulation, if the update back from the front, the value of the new state will be a reference to a new state of the line, but in fact, what we need is an old state line.

However, if the update from back to front problem does not exist, because the previous state regardless of the value, before the state must not be updated. If you do not understand that friends may wish to familiarize yourself with this dynamic programming form-filling process, I believe that is easy to understand.

This technique is a summary of the experience, if not think it does not matter, this technique as a learning knowledge points, after encountering a similar problem, the flexibility to apply.

Let's look at the code:

References:

1、https://blog.csdn.net/qq_37767455/article/details/99086678

Published 446 original articles · won praise 334 · Views 1.24 million +

Guess you like

Origin blog.csdn.net/lw_power/article/details/104138440