I think I got a solution. At least he is working on two sets of samples. It does not necessarily return the same set as the answer, but the set that it returns has the same minimum value. It is iterative and greedy, which is also nice, and there are tons of ways to optimize it. It looks like MLog (N).
It is important to understand that the numbers do not matter - only the differences between them. When you “delete a number”, you are really just combining two adjacent differences. Then my algorithm will focus on the differences. It is a simple question to go back to what elements caused these differences and remove them when you go.
Algorithm
- Create an ordered list / array of differences between each number.
- Find the lowest difference x. If the quantity x> the remaining M, we stop. You are already at best.
- For each value of x starting with the leftmost one, combine this difference with the next lower one (and delete that x). If the neighbors have equal values, your decision is arbitrary. If only one neighbor has an x value, combine it with another neighbor. (If you have no choice, for example, [1, 1, ...], then in combination with matching X, but avoid it if you can.)
- Return to step 2 until you finish M.
Algorithm Notes
In step 3, there is a point that I designated as an arbitrary solution. It probably shouldn't be, but you get into quite rare cases when it comes to how many difficulties you want to add. This arbitrariness allows us to solve several different correct answers. If you see two neighbors having the same value, at the moment I am saying randomly choose one. Ideally, you should probably check out a pair of neighbors that are at 2, then 3, etc., and prefer the lower one. I'm not sure what to do if you get to the edge when expanding. Ultimately, to make this part great, you may need to call this function recursively and see which grades are better.
Passing sample data
Second first:
Delete no more than 8 from: 0 3 7 10 15 26 38 44 53 61 76 80 88 93 100
[3, 4, 3, 5, 11, 12, 6, 9, 8, 15, 4, 8, 5, 7] M = 8
Delete 3. Edge deletions can only be added in one direction:
[7, 3, 5, 11, 12, 6, 9, 8, 15, 4, 8, 5, 7] M = 7
[7, 8, 11, 12, 6, 9, 8, 15, 4, 8, 5, 7] M = 6
Then delete 4: [7, 8, 11, 12, 6, 9, 8, 15, 12, 5, 7] M = 5
Then delete 5: [7, 8, 11, 12, 6, 9, 8, 15, 12, 12] M = 4
Then delete 6: [7, 8, 11, 12, 15, 8, 15, 12, 12] M = 3
Then delete 7: [15, 11, 12, 15, 8, 15, 12, 12] M = 2
Then delete 8: [15, 11, 12, 15, 23, 12, 12] M = 1 // note an arbitrary decision about the direction of adding
Finally, delete 11: [15, 23, 15, 23, 12, 12]
Please note that in the answer the lowest difference is 12.
First last
Delete no more than 7 from: 0 3 7 10 15 18 26 31 38 44 53 60 61 73 76 80 81 88 93 100
[3, 4, 3, 5, 3, 8, 5, 7, 6, 9, 7, 1, 12, 3, 4, 1, 7, 5, 7] M = 7
Delete 1:
[3, 4, 3, 5, 3, 8, 5, 7, 6, 9, 8, 12, 3, 4, 1, 7, 5, 7] M = 6
[3, 4, 3, 5, 3, 8, 5, 7, 6, 9, 8, 12, 3, 5, 7, 5, 7] M = 5
There are 4 3 left, so we can remove them:
[7, 3, 5, 3, 8, 5, 7, 6, 9, 8, 12, 3, 5, 7, 5, 7] M = 4
[7, 8, 3, 8, 5, 7, 6, 9, 8, 12, 3, 5, 7, 5, 7] M = 3
[7, 8, 11, 5, 7, 6, 9, 8, 12, 3, 5, 7, 5, 7] M = 2 // Note the arbitrary addition on the right
[7, 8, 11, 5, 7, 6, 9, 8, 12, 8, 5, 7, 5, 7] M = 1
We would delete the 5 following, but only 1 is allowed to be deleted, and there are 3 of them, so we stop here. Our lowest difference is 5, corresponding to the decision.
Note Edited from the idea of combining identical X values to avoid this, for case 1, 29, 30, 31, 59 presented by SauceMaster.