Sample Input:
5 120
16 00 17 00
10 30 14 30
20 45 22 15
10 00 13 15
09 00 11 00
Sample Output:
00 00 09 00
17 00 20 45
Code: (in JAVA)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
class Solution | |
{ | |
public static void main( String args[] ) | |
{ | |
Scanner in = new Scanner(System.in); | |
int N,time_wanted; | |
N=in.nextInt(); | |
time_wanted=in.nextInt(); | |
int hr,mn; | |
int start[] = new int[N]; | |
int end[] = new int[N]; | |
for(int j=0;j<N;j++) | |
{ | |
hr=in.nextInt(); | |
mn=in.nextInt(); | |
start[j]=hr*60+mn; | |
hr=in.nextInt(); | |
mn=in.nextInt(); | |
end[j]=hr*60+mn; | |
if(end[j]==0) | |
end[j]=1440; | |
} | |
schedule(start,end,N,time_wanted); | |
} | |
public static void schedule(int[] start, int[] end, int N,int time_wanted) | |
{ | |
int min=0,max=24*60,temp; | |
//sort based on start time using bubble sort | |
for(int i=N-1;i>=0;i--) | |
{ | |
for(int j=0;j<i;j++) | |
{ | |
if(start[j]>start[j+1]) | |
{ | |
temp=start[j]; | |
start[j]=start[j+1]; | |
start[j+1]=temp; | |
temp=end[j]; | |
end[j]=end[j+1]; | |
end[j+1]=temp; | |
} | |
} | |
} | |
//find the answer | |
for(int i=0;i<N;i++) | |
{ | |
if(start[i] >end[i]) | |
{ | |
continue; | |
} | |
else | |
{ | |
if(start[i]>min && start[i]<max) | |
{ | |
if(start[i]-min>=time_wanted) | |
{ | |
display(min,start[i]); | |
} | |
} | |
} | |
if(min<end[i]) | |
min=end[i]; | |
} | |
if(max-min>=time_wanted) | |
display(min,max); | |
} | |
public static void display(int min, int max) | |
{ | |
if(max==1440) | |
max=0; | |
if((min/60)/10==0) | |
System.out.print("0"+min/60+" "); | |
else | |
System.out.print(min/60+" "); | |
if(min%60 < 10) | |
System.out.print("0"+min%60+" "); | |
else | |
System.out.print(min%60+" "); | |
if((max/60)/10==0) | |
System.out.print("0"+max/60+" "); | |
else | |
System.out.print(max/60+" "); | |
if(max%60 < 10) | |
System.out.println("0"+max%60+" "); | |
else | |
System.out.println(max%60+" "); | |
} | |
} |
Please let me know if you have any questions .
2 comments:
Hi,
I tired this problem using C++
My Alogirthm is:
1) Sort the array by start time
2) calculating difference between Start time and End time of Alternate schedules.
##Note## this program passed First 7 test cases.
Can you please assist me where i did mistake..Which one i forgot to check.?
Here's my code.
#include
#include
#include
#include
using namespace std;
class meeting_Shld
{
vector slot;
int M,K,val,T,Time_sec,Start_HH=0,Start_MM=0,End_HH=24,End_MM=0;
void getData()
{
cin>>M>>K;
T=M*4;
// cout<>val;
slot.push_back(val);
}
}
void swapp(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
//cout<< """ """<<*b<<" "<<*a<<" "<slot[j])
{
swapp(&slot[i],&slot[j]);
swapp(&slot[i+1],&slot[j+1]);
swapp(&slot[i+2],&slot[j+2]);
swapp(&slot[i+3],&slot[j+3]);
}
else if(slot[i]==slot[j])
{
if(slot[i+1]>slot[j+1])
{
swapp(&slot[i],&slot[j]);
swapp(&slot[i+1],&slot[j+1]);
swapp(&slot[i+2],&slot[j+2]);
swapp(&slot[i+3],&slot[j+3]);
}
}
}
}
void display()
{
for(int i=0,j=1;i=0)
{
Time_sec=Time_sec+(slot[1]-Start_MM);
//cout<=K)
cout<=0)
{
Time_sec=Time_sec+(slot[i+3]-slot[i+1]);
if(Time_sec>=K)
cout<=0)
{
Time_sec=Time_sec+(End_MM-slot[T-1]);
//cout<=K)
cout<<setw(2)<<setfill('0')<<slot[T-2]<<" "<<setw(2)<<setfill('0')<<slot[T-1]<<" "<<setw(2)<<setfill('0')<<End_HH-24<<" "<<setw(2)<<setfill('0')<<End_MM<<endl;
}
}
//-----------------------------End of day --------------------------------------------------------------------------------------------------------------
}
public:
void get_start()
{
getData();
sortData();
//display();
//cout<<endl;
getMeeting();
}
};
int main()
{
meeting_Shld ms;
ms.get_start();
return 0;
}
Post a Comment