Skip to content

Commit

Permalink
Greedy problems solved
Browse files Browse the repository at this point in the history
  • Loading branch information
Nanashii76 authored Feb 29, 2024
1 parent 98b28f0 commit fa49686
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
31 changes: 31 additions & 0 deletions theory/data_structure/arrays.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>

//Reverse an array
void reverseArray(int *v, int size) {

int tmp, a = 0;
for(int i = size-1; i>=size/2; --i) {
tmp = v[i];
v[i] = v[a];
v[a] = tmp;
a++;
}

}

int main(){

int arr[] = {0,1,2,3,4,5,6,7,8,9};
int n = 10;
reverseArray(arr, n);

}









53 changes: 53 additions & 0 deletions theory/greedy/busyman.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <iostream>
#include <vector>
#include <algorithm>


// Creating a template to sort the vector of pairs
// using the second int element
bool sortbysec(const std::pair<int,int> &a, const std::pair<int,int> &b) {
return (a.second < b.second);
}

// function to count the max of activites that we can make
// Picking the first, after sorting te vector, to use as base
int findMax(std::vector<std::pair<int, int>> v) {

std::sort(v.begin(),v.end(),sortbysec);

int count = 1, temp = v[0].second;
for(int i = 1; i < v.size(); ++i) {
if(v[i].first >= temp && v[i].second > temp) {
count++;
temp = v[i].second;
}
}

return count;

}

int main(){


std::vector<std::pair<int, int>> act;
int n, t;
std::cin >> n;

for(int i = 0; i < n; ++i) {

std::cin >> t;
for(int j = 0; j < t; ++j) {
int s, e;
std::cin >> s >> e;
act.emplace_back(std::make_pair(s,e));
}

int count = findMax(act);
std::cout << count << std::endl;

}

}


59 changes: 59 additions & 0 deletions theory/greedy/indianCoinChange.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Problem: Suposse you want to make a change of some
Indian Rupees using minimum number of coins / notes
Denominatios = [1,2,5,10,20,50,100,500,2000]
Input
273
Output
100+100+50+20+2+1
*/

#include <iostream>
#include <vector>
#include <algorithm>

std::vector<int> denomination = {1,2,5,10,20,50,100,500,2000};


void findMin(int n) {

std::vector<int> ans;

for(int i = denomination.size()-1; i >= 0; --i) {

while(n >= denomination[i]) {
n -= denomination[i];
ans.push_back(denomination[i]);
}
}


for(auto x: ans)
std::cout << x << " ";
std::cout << std::endl;

}


int main(){

int n;
std::cin >> n;

findMin(n);


}







0 comments on commit fa49686

Please sign in to comment.