Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added mergingMeetings.cpp,multiolicationOf3Integers.cpp #1611

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<iostream>
#include<bits/stdc++.h>
#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<int> ar; //ar is for storing problem list,
for(int i = 0; i<n; i++)
{
cin>>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; i<n; i++) //loop is for finding highest and lowest
{
if(high1<ar[i])
{
high3 = high2;
high2 = high1;
high1 = ar[i];

}
else if(high2<ar[i])
{
high3 = high2;
high2 = ar[i];
}
else if(high3<ar[i])
{
high3 = ar[i];
}

if(low1>ar[i])
{
low2 = low1;
low1 = ar[i];
}
else if(low2>ar[i]){
low2 = ar[i];
}
}
ans = high3* (low1*low2<high1*high2?high1*high2:low1*low2); //getting answer
cout<< ans<<"\n";
}

int main()
{
int t; //test cases
cin>>t;
while(t--)
{
solve(); //calling solve method to print output
}
return 0;
}
117 changes: 117 additions & 0 deletions Greedy Algorithms/merging meetings/cpp/meetings.cpp
Original file line number Diff line number Diff line change
@@ -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<iostream>
#include<bits/stdc++.h>
#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()<meeting2.getStartTime();
}
void merge_meetings(vector<Meeting> &v)
{
sort(v.begin(), v.end(), comp_by_start); // sorted meetings w.r.t. starting time
for(int i=0; i<v.size()-1; i++)
{
if(v[i].getEndTime()<v[i+1].getStartTime())
{
continue;
}
else if(v[i].getEndTime()>=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<Meeting> v(n);
for(int i=0; i<n; i++)
{
cout<<"enter meeting start and end times\n";
cin>>temp1;
cin>>temp2;
Meeting meet(temp1,temp2);
v.pb(meet);
}
merge_meetings(v);
cout<<"starttime emdtime\n";
for(int i=0; i<v.size(); i++){
cout<<v[i].getStartTime()<<" "<<v[i].getEndTime()<<endl;
}
}
return 0;
}