excuse me for the confusing title, I need to implement an algorithm which can be simplified as the following:
given an array of integers, and the number of merges needed (denoted as k), return the maximum min value of the merged array, a merge can only happen with adjacent elements.
E.g. array = [3,2,8,2,9]
, k = 2
The maximum min value of the array after two merges is 5
, and the merged array is [5, 10, 9]
. In this example, we need to merge elements 3
& 2
and 8
& 2
.
Any other merge strategies will yield min val that is smaller or equal to 5
, e.g.:
[5,8,11]
, [3,10,11]
, [13,2,9]
(merged elements can be merged again)
What is the best data structure to represent the data and what is an efficient algorithm to address the problem? As far as I can think of, a greedy algorithm needs to be applied where a merge needs to happen with the current min value of the array and one of its smaller neighboring element.
Edit: I just realized that greedy algorithm might not apply, sorry for the misleading comment, if it doesn't distinguish between merging with left or right elements, this will generate the wrong answer. Take this as an example, given an array = [4,5,3,5]
, and we need to remove 2
elements.
With greedy, [4,5,3,5]
-> [4,8,5]
-> [12,5]
, so the answer is 5
; however the correct answer should be 8
with the following merge sequence:
[4,5,3,5]
-> [4,5,8]
-> [9,8]