Minimum money required to buy items with given cashbacks
Last Updated :
28 Nov, 2022
Given an array arr[] of length N, denoting the cost of N items and the cashback upon buying that item, the task is to find the minimum amount of money required to buy all the items irrespective of the order in which they are bought.
Examples:
Input: arr[] = [ [ 7, 2 ], [ 10, 5 ], [ 2, 1] ]
Output: 16
Explanation: The geek can buy the goodies in the following ways:
[7, 2] -> [10, 5] -> [2, 1] minimum money required are 15.
Initially 15. After buying the first element (15-7+2) = 10.
After buying the second element (10-10+5) = 5.
After buying the third item (5-2+1) = 4.
If anything less than 15 was used then one would not have enough money
to buy the second item. Similarly for the following cases
[7, 2] -> [2, 1] -> [10, 5] minimum money required is 16
[10, 5] -> [7, 2] -> [2, 1] minimum money required is12
[10, 5] -> [2, 1] -> [7, 2] minimum money required is 13
[2, 1] -> [7, 2] -> [10, 5] minimum money required is 16
[2, 1] -> [10, 5] -> [7, 2] minimum money required is 13
In the worst case, geek requires 16 unit of money
Input: arr[] = [ [ 5, 6 ], [40, 2], [89, 8] ]
Output: 127
Naive Approach: To solve the problem follow the below idea:
Generate all the ways possible and find the minimum money required for each permutation.
Time Complexity: O(N*N!)
Auxiliary Space: O(N)
Efficient Approach: This problem can be solved by using the below observation:
- Calculate the effective amount of money spent on items where the price ? refund. Effective amount spend = price – refund.
- Calculate the maximum price among those items where the refund > price. Effective amount spend = price of most expensive item.
- Add refund from the last transaction because we cannot use refund from the last transaction to reduce effective amount spend.
- Last transaction in worst case will be either having maximum refund such that price of that item > refund.
- Otherwise, last transaction will have maximum cost if refund on purchasing that item is greater than its price.
Follow the steps mentioned below to implement the above idea:
- Iterate through the array from i = 0 to N-1:
- Add the effective cost.
- In each iteration calculate the maximum value incurred in the last transaction in worst case using the idea mentioned above.
- At the end add the maximum cost of last transaction in worst case with the total effective cost to get the required answer.
Below is the implementation of the above idea.
C++
#include <bits/stdc++.h>
using namespace std;
long long minimumGeekBits(vector<vector< int > >& Goodies)
{
long long ans = 0;
int v = 0;
for ( int i = 0; i < Goodies.size(); i++) {
ans += max(Goodies[i][0] - Goodies[i][1], 0);
v = max(v, min(Goodies[i][0], Goodies[i][1]));
}
return ans + v;
}
int main()
{
vector<vector< int > > arr
= { { 7, 2 }, { 10, 5 }, { 2, 1 } };
cout << minimumGeekBits(arr);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static long minimumGeekBits( int Goodies[][])
{
long ans = 0 ;
int v = 0 ;
for ( int i = 0 ; i < Goodies.length; i++) {
ans += Math.max(Goodies[i][ 0 ] - Goodies[i][ 1 ],
0 );
v = Math.max(
v, Math.min(Goodies[i][ 0 ], Goodies[i][ 1 ]));
}
return ans + v;
}
public static void main(String[] args)
{
int arr[][] = { { 7 , 2 }, { 10 , 5 }, { 2 , 1 } };
System.out.print(minimumGeekBits(arr));
}
}
|
Python3
def minimumGeekBits(Goodies):
ans = 0
v = 0
l = len (Goodies)
for i in range ( 0 ,l):
ans + = max (Goodies[i][ 0 ] - Goodies[i][ 1 ], 0 )
v = max (v, min (Goodies[i][ 0 ], Goodies[i][ 1 ]))
return ans + v;
arr = [ [ 7 , 2 ], [ 10 , 5 ], [ 2 , 1 ] ]
print (minimumGeekBits(arr))
|
C#
using System;
public class HelloWorld
{
public static long minimumGeekBits( int [,] Goodies)
{
long ans = 0;
int v = 0;
for ( int i = 0; i < Goodies.GetLength(0); i++) {
ans += Math.Max(Goodies[i,0] - Goodies[i,1], 0);
v = Math.Max(v, Math.Min(Goodies[i,0], Goodies[i,1]));
}
return ans + v;
}
public static void Main( string [] args)
{
int [,] arr
= { { 7, 2 }, { 10, 5 }, { 2, 1 } };
Console.WriteLine(minimumGeekBits(arr));
}
}
|
Javascript
<script>
function minimumGeekBits(Goodies) {
let ans = 0;
let v = 0;
for (let i = 0; i < Goodies.length; i++) {
ans += Math.max(Goodies[i][0] - Goodies[i][1], 0);
v = Math.max(v, Math.min(Goodies[i][0], Goodies[i][1]));
}
return ans + v;
}
let arr
= [[7, 2], [10, 5], [2, 1]];
document.write(minimumGeekBits(arr));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...