Blog

Gold 5 Binary Search Problem Patterns

Published 1 min read

3020: Firefly

3020: Firefly

If you just consider every obstacle from 1 to N,
it's O(NH) and times out.

Knowing that the order among stalagmites and stalactites has no effect on the problem,
and since sorting can only make it easier, never harder,
let's sort first and then think.

Now with binary search
find the 'stalagmites greater than or equal to' the current height and
the 'stalactites greater than or equal to' the height when counting from the top as 1.
This can be done in O(logN),

and the overall time complexity becomes O(HlogN).

3079: Immigration

3079: Immigration

Add up the number of people each booth can process within a given time, and
run binary search with the number of people to be processed as the boundary

Comments

Leaving a comment stores your nickname (or GitHub profile info). Privacy policy

GitHub