Upcoming SlideShare
×

# Avid tv watcher

3,214 views

Published on

Published in: Technology, Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

• Be the first to like this

Views
Total views
3,214
On SlideShare
0
From Embeds
0
Number of Embeds
1,792
Actions
Shares
0
17
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Avid tv watcher

1. 1. Avid TV Watcher<br />Problem:<br />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.<br />Algorithm:<br /><ul><li>Merge all the programs of diff channels in sorted order of their end-time in prog[] array.
2. 2. n ← length(prog) - 1
3. 3. for i ← 1 to n
4. 4. do prog[i].cnt ← 0
5. 5. max ← 0
6. 6. j ← 0
7. 7. while prog[j].end < prog[i].start
8. 8. if max < prog[j].cnt
9. 9. do max ← proj[j].cnt
10. 10. j ← j + 1
11. 11. do prog[i].cnt ← max + prog[i].end – prog[i].start
12. 12. do res ← 0
13. 13. for i ← 1 to n
14. 14. if res < prog[i].cnt
15. 15. do res ← prog[i].cnt
16. 16. return res</li></ul>Example:<br />Channel 1:<br />Program idP1P2P3Start time8:009:0010:30End time8:3010:0011:30<br />Channel 2:<br />Program idP4P5P6Start time8:159:3010:45End time9:1510:1511:15<br />Sort all programs based on their end time:<br />Cnt000000Pr idP1P4P2P5P6P3St time8:008:159:009:3010:4510:30End time8:309:1510:0010:1511:3011:30<br />1st Iteration:<br />Cnt00:3000000Pr idP1P4P2P5P6P3St time8:008:159:009:3010:4510:30End time8:309:1510:0010:1511:3011:30<br />2nd Iteration:<br />Cnt00:3001:000000Pr idP1P4P2P5P6P3St time8:008:159:009:3010:4510:30End time8:309:1510:0010:1511:3011:30<br />3rd Iteration:<br />Cnt00:3001:0001:30000Pr idP1P4P2P5P6P3St time8:008:159:009:3010:4510:30End time8:309:1510:0010:1511:3011:30<br />4th Iteration:<br />Cnt00:3001:0001:3001:4500Pr idP1P4P2P5P6P3St time8:008:159:009:3010:4510:30End time8:309:1510:0010:1511:3011:30<br />5th Iteration:<br />Cnt00:3001:0001:3001:4502:300Pr idP1P4P2P5P6P3St time8:008:159:009:3010:4510:30End time8:309:1510:0010:1511:3011:30<br />6th Iteration:<br />Cnt00:3001:0001:3001:4502:3002:45Pr idP1P4P2P5P6P3St time8:008:159:009:3010:4510:30End time8:309:1510:0010:1511:3011:30<br />