-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGreedy.cpp
More file actions
43 lines (39 loc) · 834 Bytes
/
Greedy.cpp
File metadata and controls
43 lines (39 loc) · 834 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<bits/stdc++.h>
using namespace std;
//Min coins for a particular amount
int minCoins(int coins[], int n, int amount){
sort(coins, coins + n, greater<int>());
int res = 0;
for(int i = 0; i < n; i++){
if(coins[i] <= amount){
int c = floor(amount/coins[i]);
res += c;
amount -= c*coins[i];
}
if(amount == 0){
break;
}
}
return res;
}
//Activity Selection Problem
void printMaxActivities(int s[], int f[], int n)
{
int i, j;
i = 0;
cout << i;
for (j = 1; j < n; j++)
{
if (s[j] >= f[i])
{
cout << j;
i = j;
}
}
}
int main(){
int arr[] = {5, 2, 10, 1};
int n = sizeof(arr) / sizeof(arr[0]);
cout << minCoins(arr, n, 52);
return 0;
}