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

📅  最后修改于: 2021-10-27 06:25:50             🧑  作者: 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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程