给定整数N对和整数K,任务是找到降低的最小数目需要使得每对的第一元素的总和为≤ķ。每次减少涉及减小的一对的第一值到它的第二值。如果它是不可能使之和≤K,打印-1。
Examples:
Input: N = 5, K = 32
10 6
6 4
8 5
9 8
5 2
Output: 2
Explanation:
Total Sum = 10 + 6 + 8 + 9 + 5 = 38 > K
Reducing 10 – > 6 and 8 – > 5 reduces the sum to 31( 6 + 6 + 5 + 9 + 5) which is less than K.
Input: N = 4, K = 25
10 5
20 9
12 10
4 2
Output: -1
方法:
请按照以下步骤解决问题:
- 计算每对第一个元素的总和。如果总和已经≤K,则打印0。
- 根据它们之间的差异对给定的对进行排序。
- 计算需要以非递增顺序添加的对对的差异数,以使总和小于K。
- 如果遍历所有对之后总和超过K,则打印-1。否则,打印计数。
下面是上述方法的实现:
C++
// C++ Program to find the count of
// minimum reductions required to
// get the required sum K
#include
using namespace std;
// Function to return the count
// of minimum reductions
int countReductions(
vector >& v,
int K)
{
int sum = 0;
for (auto i : v) {
sum += i.first;
}
// If the sum is already
// less than K
if (sum <= K) {
return 0;
}
// Sort in non-increasing
// order of difference
sort(v.begin(), v.end(),
[&](
pair a,
pair b) {
return (a.first - a.second)
> (b.first - b.second);
});
int i = 0;
while (sum > K && i < v.size()) {
sum -= (v[i].first
- v[i].second);
i++;
}
if (sum <= K)
return i;
return -1;
}
// Driver Code
int main()
{
int N = 4, K = 25;
vector > v(N);
v[0] = { 10, 5 };
v[1] = { 20, 9 };
v[2] = { 12, 10 };
v[3] = { 4, 2 };
// Function Call
cout << countReductions(v, K)
<< endl;
return 0;
}
Java
// Java program to find the count of
// minimum reductions required to
// get the required sum K
import java.util.*;
class GFG{
// Function to return the count
// of minimum reductions
static int countReductions(ArrayList v,
int K)
{
int sum = 0;
for(int[] i : v)
{
sum += i[0];
}
// If the sum is already
// less than K
if (sum <= K)
{
return 0;
}
// Sort in non-increasing
// order of difference
Collections.sort(v, (a, b) -> Math.abs(b[0] - b[1]) -
Math.abs(a[0] - a[1]));
int i = 0;
while (sum > K && i < v.size())
{
sum -= (v.get(i)[0] - v.get(i)[1]);
i++;
}
if (sum <= K)
return i;
return -1;
}
// Driver code
public static void main(String[] args)
{
int N = 4, K = 25;
ArrayList v = new ArrayList<>();
v.add(new int[] { 10, 5 });
v.add(new int[] { 20, 9 });
v.add(new int[] { 12, 10 });
v.add(new int[] { 4, 2 });
// Function Call
System.out.println(countReductions(v, K));
}
}
// This code is contributed by offbeat
Python3
# Python3 program to find the count of
# minimum reductions required to
# get the required sum K
from typing import Any, List
# Function to return the count
# of minimum reductions
def countReductions(v: List[Any], K: int) -> int:
sum = 0
for i in v:
sum += i[0]
# If the sum is already
# less than K
if (sum <= K):
return 0
# Sort in non-increasing
# order of difference
v.sort(key = lambda a : a[0] - a[1])
i = 0
while (sum > K and i < len(v)):
sum -= (v[i][0] - v[i][1])
i += 1
if (sum <= K):
return i
return -1
# Driver Code
if __name__ == "__main__":
N = 4
K = 25
v = [[0, 0] for _ in range(N)]
v[0] = [10, 5]
v[1] = [20, 9]
v[2] = [12, 10]
v[3] = [4, 2]
# Function Call
print(countReductions(v, K))
# This code is contributed by sanjeev2552
输出:
-1
时间复杂度: O(NlogN)
辅助空间: O(1)