Avid tv watcher

Post on 21-Nov-2014

1.294 views 5 download

description

 

Transcript of Avid tv watcher

Avid TV Watcher

Problem:There is a TV avid person, who wants to spend his maximum time on TV. There are N channels that telecast programs of different length at different timings. WAP to find the program and channel number so that the person can spend his max time on TV.

Algorithm:

1. Merge all the programs of diff channels in sorted order of their end-time in prog[] array.

2. n ← length(prog) - 1

3. for i ← 1 to n

a. do prog[i].cnt ← 0

b. max ← 0

c. j ← 0

d. while prog[j].end < prog[i].start

i. if max < prog[j].cnt

1. do max ← proj[j].cnt

ii. j ← j + 1

e. do prog[i].cnt ← max + prog[i].end – prog[i].start

4. do res ← 0

5. for i ← 1 to n

a. if res < prog[i].cnt

i. do res ← prog[i].cnt

6. return res

Example:

Channel 1:Program id P1 P2 P3Start time 8:00 9:00 10:30End time 8:30 10:00 11:30

Channel 2:Program id P4 P5 P6Start time 8:15 9:30 10:45End time 9:15 10:15 11:15

Sort all programs based on their end time:Cnt 0 0 0 0 0 0Pr id P1 P4 P2 P5 P6 P3St time 8:00 8:15 9:00 9:30 10:45 10:30End time 8:30 9:15 10:00 10:15 11:30 11:30

1st Iteration:Cnt 00:30 0 0 0 0 0Pr id P1 P4 P2 P5 P6 P3St time 8:00 8:15 9:00 9:30 10:45 10:30End time 8:30 9:15 10:00 10:15 11:30 11:30

2nd Iteration:Cnt 00:30 01:00 0 0 0 0Pr id P1 P4 P2 P5 P6 P3St time 8:00 8:15 9:00 9:30 10:45 10:30End time 8:30 9:15 10:00 10:15 11:30 11:30

3rd Iteration:Cnt 00:30 01:00 01:30 0 0 0Pr id P1 P4 P2 P5 P6 P3St time 8:00 8:15 9:00 9:30 10:45 10:30End time 8:30 9:15 10:00 10:15 11:30 11:30

4th Iteration:Cnt 00:30 01:00 01:30 01:45 0 0Pr id P1 P4 P2 P5 P6 P3St time 8:00 8:15 9:00 9:30 10:45 10:30End time 8:30 9:15 10:00 10:15 11:30 11:30

5th Iteration:Cnt 00:30 01:00 01:30 01:45 02:30 0Pr id P1 P4 P2 P5 P6 P3St time 8:00 8:15 9:00 9:30 10:45 10:30End time 8:30 9:15 10:00 10:15 11:30 11:30

6th Iteration:Cnt 00:30 01:00 01:30 01:45 02:30 02:45Pr id P1 P4 P2 P5 P6 P3St time 8:00 8:15 9:00 9:30 10:45 10:30End time 8:30 9:15 10:00 10:15 11:30 11:30