📌  相关文章
📜  合并数组的K个最小元素,直到只有一个元素

📅  最后修改于: 2021-04-29 12:00:06             🧑  作者: Mango

给定一个数组arr []和一个整数K ,任务是合并数组的K个最小元素,直到数组中只剩下一个元素。

注意:如果不可能只合并为一个元素,则打印-1。

方法:想法是对数组进行排序,然后将数组的前K个最小元素合并为一个元素,然后在二进制搜索的帮助下将元素按其排序位置插入数组中。同样,重复此步骤,直到数组中仅剩一个元素。如果最后剩下的元素少于K,则返回-1。

下面是上述方法的实现:

Java
// Java implementation to merge the
// K minimum elements of the Array
// until there is only one element
// is left in the array
  
// Imports
import java.io.*;
import java.lang.*;
import java.util.*;
  
  
class GFG {
      
    // Function to merge the element
    // of the array until there is
    // only one element left in array
    public static int mergeStones(
        List list, final int k) {
          
        // Sorting the array
        Collections.sort(list);
        int cost = 0;
          
        // Loop to merge the elements
        // until there is element
        // greater than K elements
        while(list.size() > k) {
            int sum = 0;
              
            // Merging the K minimum 
            // elements of the array
            for(int i = 0; i < k; i++) {
                sum += list.get(i);
            }
              
            // Removing the K minimum 
            // elements of the array
            list.subList(0, k).clear();
              
            // Inserting the merged 
            // element into the array
            insertInSortedList(list, sum); 
            cost += sum;
        }
          
        // If there is only K element
        // left then return the element
        if(list.size() == k) {
            cost += list.stream().reduce(
                         0, Integer::sum);
            return cost;
        } else {
            return -1;
        }
    }
      
    // Function insert the element into
    // the sorted position in the array
    public static void insertInSortedList(
        List sortedList, int item) {
        int len = sortedList.size();
        insertInSortedList(sortedList, item, 
                                 0, len - 1);
    }
      
    // Utility function to insert into the
    // array with the help of the position
    public static void insertInSortedList(
        List sortedList, int item, 
                       int start, int end) {
        int mid = (int) ((end - start)/ 2.00);
        if(mid == 0 || 
             (mid == sortedList.size() - 1) || 
                sortedList.get(mid) == item) {
            sortedList.add(mid, item);
            return;
        } 
        else if(sortedList.get(mid) < item) {
            insertInSortedList(sortedList, 
                       item, mid + 1, end);
        } else {
            insertInSortedList(sortedList, 
                     item, start, mid - 1);
        }
    }
      
    // Driver Code
    public static void main(String [] args) {
        List stones = new ArrayList<>();
        stones.add(3);
        stones.add(2);
        stones.add(4);
        stones.add(1);
        System.out.println(mergeStones(stones, 3));
        System.out.println(mergeStones(stones, 2));
    }
}


输出:
-1
10