Minimum number of days required to solve a list of questions

前端 未结 4 546
死守一世寂寞
死守一世寂寞 2020-12-05 21:59

There are N problems numbered 1..N which you need to complete. You\'ve arranged the problems in increasing difficulty order, and the ith problem has estimated difficulty lev

4条回答
  •  囚心锁ツ
    2020-12-05 22:44

    Do we really need to go to path cover? Can't we just follow similar strategy as LIS.

    The input is in increasing order of complexity. We just have to maintain a bunch of queues for tasks to be performed each day. Every element in the input will be assigned to a day by comparing the last elements of all queues. Wherever we find a difference of 'k' we append the task to that list.

    For ex: 5 3 4 5 6

    1) Input -> 5 (Empty lists so start a new one)

    5

    2) 3 (only list 5 & abs(5-3) is 2 (k) so append 3)

    5--> 3

    3) 4 (only list with last vi, 3 and abs(3-4) < k, so start a new list)

    5--> 3

    4

    4) 5 again (abs(3-5)=k append)

    5-->3-->5

    4

    5) 6 again (abs(5-6) < k but abs(4-6) = k) append to second list)

    5-->3-->5

    4-->6

    We just need to maintain an array with last elements of each list. Since order of days (when tasks are to be done is not important) we can maintain a sorted list of last tasks therefore, searching a place to insert the new task is just looking for the value abs(vi-k) which can be done via binary search.

    Complexity:

    The loop runs for N elements. In the worst case , we may end up querying ceil value using binary search (log i) for many input[i].

    Therefore, T(n) < O( log N! ) = O(N log N). Analyse to ensure that the upper and lower bounds are also O( N log N ). The complexity is THETA (N log N).

提交回复
热议问题