给定一个大小为 N 的数组arr[]和一个数字 K,任务是找到最小子序列的长度,使得子序列的总和大于或等于数字 K。
例子:
Input: arr[] = {2, 3, 1, 5, 6, 3, 7, 9, 14, 10, 2, 5}, K = 35
Output: 4
Smallest subsequence with the sum greater than or equal to the given sum K is {7, 9, 14, 10}
Input: arr[] = {1, 2, 2, 2, 3, 4, 5, 4, 7, 6, 5, 12}, K = 70
Output:-1
Subsequence with sum greater than equal to the given sum is not possible.
方法:
- 这个问题可以借助优先队列来解决
- 遍历输入数组并将每个数组元素插入优先级队列。
- 初始化保存从优先队列中选取元素的总和的变量,以及使从优先队列中选取元素的计数为 0 的变量
- 将元素从优先队列中弹出,直到优先队列不为空
- 将元素添加到总和中
- 增加计数,因为元素被选中以贡献总和
- 检查总和是否大于给定的数字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