📌  相关文章
📜  最小子序列的长度,使得元素的总和大于等于 K

📅  最后修改于: 2021-09-05 08:48:01             🧑  作者: Mango

给定一个大小为 N 的数组arr[]和一个数字 K,任务是找到最小子序列的长度,使得子序列的总和大于或等于数字 K。
例子:

方法:

  • 这个问题可以借助优先队列来解决
  • 遍历输入数组并将每个数组元素插入优先级队列。
  • 初始化保存从优先队列中选取元素的总和的变量,以及使从优先队列中选取元素的计数为 0 的变量
  • 将元素从优先队列中弹出,直到优先队列不为空
    1. 将元素添加到总和中
    2. 增加计数,因为元素被选中以贡献总和
    3. 检查总和是否大于给定的数字K ,如果是则停止检查并输出计数。

下面是上述方法的实现。

C++
// C++ implementation to find length of smallest
// subsequence such that sum of elements
// is greater than equal to given number K
 
#include 
using namespace std;
 
// Function to find the smallest
// subsequence such that sum of elements
// is greater than equal to given number K
int lengthOfSmallestSubsequence(int K, vector v)
{
    // Initialize priority queue
    priority_queue pq;
 
    // Loop to insert all elements into
    // the priority queue
    for (int i = 0; i < v.size(); i++) {
        pq.push(v[i]);
    }
    int sum = 0, count = 0;
 
    // Loop to find the smallest
    // subsequence such that sum of elements
    // is greater than equal to given number K
    while (!pq.empty() && sum < K) {
        sum += pq.top();
        pq.pop();
        count++;
    }
    // If sum is less then K
    // then return -1 else return count.
    if (sum < K) {
        return -1;
    }
    return count;
}
 
// Driver code
int main()
{
 
    vector v{ 2, 3, 1, 5,
                   6, 3, 7, 9,
                   14, 10, 2, 5 };
    int K = 35;
 
    cout << lengthOfSmallestSubsequence(K, v);
 
    return 0;
}


Java
// Java implementation to find length of smallest
// subsequence such that sum of elements
// is greater than equal to given number K
import java.util.*;
 
class GFG
{
 
// Function to find the smallest
// subsequence such that sum of elements
// is greater than equal to given number K
static int lengthOfSmallestSubsequence(int K, int []v)
{
    // Initialize priority queue
    Queue pq =
            new PriorityQueue(Collections.reverseOrder());
 
    // Loop to insert all elements into
    // the priority queue
    for (int i = 0; i < v.length; i++)
    {
        pq.add(v[i]);
    }
    int sum = 0, count = 0;
 
    // Loop to find the smallest
    // subsequence such that sum of elements
    // is greater than equal to given number K
    while (!pq.isEmpty() && sum < K)
    {
        sum += pq.peek();
        pq.remove();
        count++;
    }
     
    // If sum is less then K
    // then return -1 else return count.
    if (sum < K)
    {
        return -1;
    }
    return count;
}
 
// Driver code
public static void main(String[] args)
{
    int []v = { 2, 3, 1, 5,
                6, 3, 7, 9,
                14, 10, 2, 5 };
    int K = 35;
    System.out.print(lengthOfSmallestSubsequence(K, v));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation to find length of smallest
# subsequence such that sum of elements
# is greater than equal to given number K
 
# Function to find the smallest
# subsequence such that sum of elements
# is greater than equal to given number K
def lengthOfSmallestSubsequence(K, v):
     
    # Initialize priority queue
    pq = []
 
    # Loop to insert all elements into
    # the priority queue
    for i in v:
        pq.append(i)
    pq.sort()
 
    sum = 0
    count = 0
 
    # Loop to find the smallest
    # subsequence such that sum of elements
    # is greater than equal to given number K
    while (len(pq) > 0 and sum < K):
        sum += pq[-1]
        del pq[-1]
        count += 1
     
    # If sum is less then K
    # then return -1 else return count.
    if (sum < K):
        return -1
    return count
 
# Driver code
v = [2, 3, 1, 5,
    6, 3, 7, 9,
    14, 10, 2, 5]
K = 35
 
print(lengthOfSmallestSubsequence(K, v))
 
# This code is contributed by mohit kumar 29


C#
// C# implementation to find length of smallest
// subsequence such that sum of elements
// is greater than equal to given number K using System;
using System;
using System.Collections.Generic;
using System.Linq;
class GFG
{
 
  // Function to find the smallest
  // subsequence such that sum of elements
  // is greater than equal to given number K
  static int lengthOfSmallestSubsequence(int K, int []v)
  {
 
    // Initialize List
    List pq = new List();
 
    // Loop to insert all elements into
    // the List
    for (int i = 0; i < v.Length; i++) 
    {
      pq.Add(v[i]);
    }
 
    // Sort list in reverse order
    pq.Sort();
    pq.Reverse();
    int sum = 0;
    int count = 0;
 
    // Loop to find the smallest
    // subsequence such that sum of elements
    // is greater than equal to given number K
    while(pq.Count > 0 && sum < K)
    {
      sum += pq[0];
      pq.RemoveAt(0);
      count++;
    }
 
    // If sum is less then K
    // then return -1 else return count.
    if (sum < K)
    {
      return -1;
    }
    return count;
  }
 
  // Driver code
  static public void Main ()
  {
    int []v = { 2, 3, 1, 5,6, 3, 7, 9, 14, 10, 2, 5 };
    int K = 35;
    Console.WriteLine(lengthOfSmallestSubsequence(K, v));
  }
}
 
// This code is contributed by avanitrachhadiya2155


Javascript


输出:
4

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live