Leetcode-517 Super Washing Machines

Source: Internet
Author: User

Today, we begin to regularly record the interesting topics that people encounter when they brush their leetcode.
517 Super Washing machines you has n Super washing machines on a line. Initially, each washing machine have some dresses or is empty.
For each move, you could choose any m (1≤m≤n) washing machines, and pass one dress of each washing TS adjacent washing machines at the same time.
Given an integer array representing the number of dresses in each washing machine from left to right on the line, you shou LD Find the minimum number of moves to make all the washing machines has the same number of dresses. If It isn't possible to do it, return-1.
At first glance, we may think of whether we can solve the problem with DP, after all, like the question of the most value, it is easy to associate with DP. That's the way it is.
In the first step, we determine whether all machines can be successfully assigned to the same group M. It's easy to judge, not to repeat it here. With this step, we can get the final number of machine per heap (if it can be successfully assigned to the same group M), remember N.
Next, let's look at how to break this problem down into sub-problems, so it's easy to solve with DP. We start by looking at the first element of the array.
In the first case, the element is =n. This indicates that the first heap has no need to do any more moving. We only need a solution to the remaining array (from the second start to the end), and the solution to the sub-array is the solution to our whole problem.
In the second case, the element >n. This situation shows that we need to remove enough machine from the first heap, move it to the next second heap, and count the number of moves as N1. Then, the remaining sub-arrays are solved recursively, assuming the resulting solution is N2. At this point we observe the problem, the movement in different heaps is independent. For example, heap 1 moves 1 machine to heap 2, heap 2 moves 11 machine to heap 3 ... This moving sequence is only recorded as one time. With this conclusion, we return to the second case where N1 and N2 are also independent. So our solution in this case is Max (N1,N2).
In the third case, the element <n. In this case, we need to remove enough machine from the second heap to move it to the first heap. What if the number of the second pile is still not enough? Then take it from the third heap, and so on. We can use an example to illustrate. 0,1,3,14,2 This example ends up with a quantity of 4 per heap. However the first heap = 0, then we need to take 4 machine (N1) from the second heap, but the second heap is 1, or not enough. And the second pile of its own final number must also be 4, then it needs a total of 7 from the third heap (N2). The third heap, in other words, needs to fetch 8 (N3) from the Forth heap to meet the needs of all the heaps on the left. To the heap four, we found that it was enough, and no longer had to take machine to its right. So we can calculate the number of moves required by a loop NX = Max (N1,n2 ...). Then the sub-problem of the remaining sub-arrays is solved recursively, and the solution is the NY. For this example, the sub-problem is the solution of the sub-array starting with the fourth element of the array. See here, perhaps our solution can be thought of, is Max (Nx,ny)? However, that is not the case. Read the problem again, the operation on different heaps is really independent. But for the same heap, this is not the case. Still observing the previous example, the fourth heap needs to move 8 elements into the third heap, and it also needs to move 2 elements to the fifth heap on the right. So the solution to the whole problem is the 8+2=10 heap, not the 8 heap of Max (8,2), because the operation on the same heap is not independent. So when we design a recursive function, in addition to returning the minimum number of moves for the entire operation, we must also return the move number of the boundary element, so that the solution of the main problem can be computed when the sub-problem returns. Paste the code:
intFindminmoves (vector<int>&machines) {    if(Machines.empty ())return 0; intsum = accumulate (begin (machines), End (machines),0); if(Sum% machines.size () = =0) {        inttarget = SUM/machines.size (); returnFindminmoves (Machines,0, Target). First; }    Else return-1;}//back to Pair<minmove, boundarymove>//Boundarymove Indicates the number of moves from the start position and the start+1 position (bidirectional)pair<int,int> findminmoves (vector<int>& machines,intStartinttarget) {    if(Start = = Machines.size ()-1) {        returnMake_pair (0,0); }    if(Target <=Machines[start]) {        intmove = Machines[start]-Target; Machines[start+1] +=move; intMinmove = Findminmoves (machines, start +1, Target). First; returnMake_pair (Max (move, Minmove), move); }    Else {        inti =start; intMaxmove =0; intmove;  Do{Move= Target-Machines[i]; Maxmove=Max (Maxmove, move); Machines[i+1] -=move; I++; }  while(Machines[i] <target); Pair<int,int> REM =findminmoves (machines, I, target); intRmove =Rem.first; Maxmove=Max (Maxmove, Rmove); Maxmove= Max (Maxmove, move +Rem.second); returnMake_pair (Maxmove, Target-Machines[i]); }}

Leetcode-517 Super Washing Machines

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.