Removal of billboards from given ones

前端 未结 7 1908
Happy的楠姐
Happy的楠姐 2020-12-14 11:46

I came across this question

ADZEN is a very popular advertising firm in your city. In every road you can see their advertising billboards. Recentl

7条回答
  •  借酒劲吻你
    2020-12-14 12:23

    This problem is one of the challenges posted in www.interviewstreet.com ...

    I'm happy to say I got this down recently, but not quite satisfied and wanted to see if there's a better method out there.

    soulcheck's DP solution above is straightforward, but won't be able to solve this completely due to the fact that K can be as big as N, meaning the DP complexity will be O(NK) for both runtime and space.

    Another solution is to do branch-and-bound, keeping track the best sum so far, and prune the recursion if at some level, that is, if currSumSoFar + SUM(a[currIndex..n)) <= bestSumSoFar ... then exit the function immediately, no point of processing further when the upper-bound won't beat best sum so far.

    The branch-and-bound above got accepted by the tester for all but 2 test-cases. Fortunately, I noticed that the 2 test-cases are using small K (in my case, K < 300), so the DP technique of O(NK) suffices.

提交回复
热议问题