SRM: 477 - DIV: II - 250 Problem - Vacation Time

  • 7

The problem main idea was to choose an interval to ensure the minimum re-scheduled days through the whole No. of days given
You can view the whole problem here

The first approach to this problem is brute force algorithm, we may think of it this way;
may be we should try all the possible intervals that start at day no [ i ] and end at day [ j ]
and count the no of re-scheduled days between those days, and keep track of the minimum no of scheduled days.

This algorithm will satisfy the time limit but is still as obvious in O ( n ^ 2 ).

another Dynamic Solution will solve problem in only O( n ).

the hint is we may solve this problem as follows ;

1st - we will have an Array that holds at Position [ i ] ; the sum of all scheduled days from day [1] to day [ i ]. that is by simply looping through the array and accumlating the sum of the previous day to this day we are in.

2nd - we loop again through the same array checking the minimum scheduled days in between day[ i ] and day [ i + K ].

Simply we transformed the too nested loops into two separate loops that each run in O ( n ) (^.^)
No one believe what Dynamic Programming can do to your life :)

Here is the sample code for a more clarification.

7 comments :

  1. m3 en elms2la di leha zkryat we7shaa bs nice work .....

    ReplyDelete
  2. I see that this is a good start. Good analysis, good idea to use dynamic programming.

    But try to have a good posting rate, we see a lot of guys stopping after the second post, so keep up. :)

    ReplyDelete
  3. Thanks all :) i hope it will be like this and better isa :)

    ReplyDelete
  4. Good analysis for the problem .. Keep going :D

    ReplyDelete