Why is the greedy algorithm optimal? -


coding, lesson 14, task tyres (). In short, the problem is to divide a list of positive integers to A to the maximum number of max (prevailing) subpopulations with the least of .

I have come with only one greedy solution because this is the name of the lesson it passes through all trials but I do not know why it is an optimal solution (if it is optimal).

  int solution (int k, vector & lt; int & gt; & amp; a) {int sum = 0, count = 0; For (int a: a) {sum + = a; If (amount> = K) {++ calculation; Yoga = 0; }} Return calculation; }   

Can anyone tell me why and why this solution is optimal?

Maybe I am naïve or something wrong here, but I think it is not very difficult (Although not clear) to see if the algorithm is actually optimal.

Assume that you have a partition of the optimal list that is with the maximum number of subpopulations, you can not have all the elements in the list, but after adding one element to a valid list, If the list is generated, suppose that any possible "remaining" element was initially not assigned to any sublist that was arbitrarily assigned one of its adjacent novels; So we have a proper optimal division of the list, which we call P1.

Now think about the division, which will create greedy algorithms, say P2. There can be two things for the first sub-bill in P2:

  1. It can be similar to the first novelist in P1.
  2. This may be less than the sublist in P1. In

    1. You can repeat the logic in the next element after the next sublist. If each subsequent novel produced by algorithms is equal to P1, then P1 and P2 will be equal.

    In 2 You also repeat the argument, but now you have at least one "additional" item available, then, the next novelist can be:

    2.1. Get P1 as the next subtitle.
    2.2. Before P. 1 in the next abbillist

    And repeat Therefore, in each case, you will have several submissions in the form of at least PPA which means that P2, at least is any possible in the list Partition is good as well, and, in particular, any optimal division.

    This is not a very formal demonstration, but I think thinks is valid. Please tell anything that you think may be wrong.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -