From 65aec17dd9ab80a8d299d73c13d110edc409049c Mon Sep 17 00:00:00 2001 From: Kishorsarswat Date: Thu, 22 Oct 2020 13:22:36 +0530 Subject: [PATCH] added mergingMeetings.cpp,multiolicationOf3Integers.cpp --- .../cpp/multiplication of 3 intigers.cpp | 70 +++++++++++ .../merging meetings/cpp/meetings.cpp | 117 ++++++++++++++++++ 2 files changed, 187 insertions(+) create mode 100644 Greedy Algorithms/maximising multiplication of 3 intigers/cpp/multiplication of 3 intigers.cpp create mode 100644 Greedy Algorithms/merging meetings/cpp/meetings.cpp diff --git a/Greedy Algorithms/maximising multiplication of 3 intigers/cpp/multiplication of 3 intigers.cpp b/Greedy Algorithms/maximising multiplication of 3 intigers/cpp/multiplication of 3 intigers.cpp new file mode 100644 index 000000000..631123fc1 --- /dev/null +++ b/Greedy Algorithms/maximising multiplication of 3 intigers/cpp/multiplication of 3 intigers.cpp @@ -0,0 +1,70 @@ +//problem +//Given a vector of integers, find the highest product you can get from three of the integers. +//The input vectorOfInts will always have at least three integers. + +//solution +// we want to do it in O(n) instead of O(n3) or O(nlgn) +//so we need to follow greedy approach +//we are storing 3 highest as well as 2 lowest values to get highest multiplication result +//as 2 negative values can also be positive after multiplication +#include +#include +#define pb push_back +#define INF 1e9 +#define ll long long +using namespace std; + +void solve() +{ + int n,temp; //n = no. of values in list, temp is used for input + cin>>n; + vector ar; //ar is for storing problem list, + for(int i = 0; i>temp; + ar.pb(temp); //input + } + int low1= INF; + ll low2=INF, high1=-INF, high2=-INF, high3=-INF, ans=1; //storing maximum as well as lowest + for(int i=0; iar[i]) + { + low2 = low1; + low1 = ar[i]; + } + else if(low2>ar[i]){ + low2 = ar[i]; + } + } + ans = high3* (low1*low2>t; + while(t--) + { + solve(); //calling solve method to print output + } + return 0; +} diff --git a/Greedy Algorithms/merging meetings/cpp/meetings.cpp b/Greedy Algorithms/merging meetings/cpp/meetings.cpp new file mode 100644 index 000000000..9d7b10a61 --- /dev/null +++ b/Greedy Algorithms/merging meetings/cpp/meetings.cpp @@ -0,0 +1,117 @@ +//problem +//Your company built an in-house calendar tool called HiCal. You want to add a feature to see the times in a day when everyone is available. +//To do this, you’ll need to know when any team is having a meeting. In HiCal, a meeting is stored as an instance of a Meeting class with integer member variables startTime and endTime. These integers represent the number of 30-minute blocks past 9:00am. + +//solution +//as we need to get common free time of all employes, we need to merge given meeting timesand find- +//free slots between them +//step1: get sorted your list w.r.t. starting time. +//step2: compare their ending times. +//step3: merge meetings in place and get final list. +#include +#include +#define pb push_back +#define INF 1e9 +#define ll long long +using namespace std; + +class Meeting //meeting obects will be elements of list +{ +private: + // number of 30 min blocks past 9:00 am + unsigned int startTime_; + unsigned int endTime_; + +public: + Meeting() : + startTime_(0), + endTime_(0) + { + } + + Meeting(unsigned int startTime, unsigned int endTime) : + startTime_(startTime), + endTime_(endTime) + { + } + + unsigned int getStartTime() const + { + return startTime_; + } + + void setStartTime(unsigned int startTime) + { + startTime_ = startTime; + } + + unsigned int getEndTime() const + { + return endTime_; + } + + void setEndTime(unsigned int endTime) + { + endTime_ = endTime; + } + + bool operator==(const Meeting& other) const + { + return + startTime_ == other.startTime_ + && endTime_ == other.endTime_; + } +}; + +bool comp_by_start(Meeting &meeting1, Meeting &meeting2) // used for sorting with starting time +{ + return meeting1.getStartTime() &v) +{ + sort(v.begin(), v.end(), comp_by_start); // sorted meetings w.r.t. starting time + for(int i=0; i=v[i+1].getEndTime()) + { + v.erase(v.begin()+1+i); + i--; + } + else{ + v[i].setEndTime(v[i+1].getEndTime()); + v.erase(v.begin()+1+i); + i--; + } + } +} + +int main() +{ + int t; //test cases + cin>>t; + while(t--) + { + unsigned int n, temp1, temp2; + cout<<"enter no. of meetings"; + cin>>n; + vector v(n); + for(int i=0; i>temp1; + cin>>temp2; + Meeting meet(temp1,temp2); + v.pb(meet); + } + merge_meetings(v); + cout<<"starttime emdtime\n"; + for(int i=0; i