2

So as I was going through some practice problems from programming contests (ACM ICPC, etc), people can often times take an O(N^2) Solution, or even worse, and use a Heap (priority_queue in C++) or a deque to reduce complexity. (as some sort of optimization, after noticing "something" in the pattern)

For example in the "sliding window maximum" problem, which is pretty much:

For each window of size K in an array of size N, compute the maximum.

There's a trivial O(NK) algorithm, a rather easy O(nlogn) solution (even I can see it, using a heap) and a O(N) solution, using a double ended queue.

These principals seem to be on the principal of "throwing" away useless values, or querying a region to find a property (maximum, cumulative sum, min, etc).

For example to convert some O(N^2) algorithms to O(NlogN), sometimes you can use a priority_queue and keep popping out values until you get one within a certain window, instead of looping through all previous N elements to find a maximum.

Anyone have good tips? (Other than doing more problems... I'm trying to do that)

1 Answer 1

0

Basic of DP Algorithm is splitting problem.

In order to reducing time complexity, let's split problem in a different way.

To embodiment DP algorithm, we use many easy sub-algorithms such as sort, tree(even it's not algorithm), ...

If you want to reducing time complexity, embody this algorithms more fast.

If you are using sort, use quick sort or heap sort instead of selection/bubble sort.

If you want to get min/max value, use heap or priority queue.

If you can't make more fast recurrence formula, then reduce time using more fast sub-algorithms.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.