Блог пользователя Boshkash_Hates_CP

Автор Boshkash_Hates_CP, история, 19 месяцев назад, По-английски

Hello , when solving problems on binary search, i solve them because i know that i'm solving problems on Binary search topic , but if i come to a problem statement i don't know how or even if I should use binary search or not ... so my question is , how to make sure you're solving a binary search problem?

  • Проголосовать: нравится
  • +11
  • Проголосовать: не нравится

»
19 месяцев назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

solve more problems, work on more ideas, see the editorial. after some months you will be able to identify it.

»
19 месяцев назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

You should read about monotonic functions and try to find patterns similar to it. Also finding minimum or maximum value type problems may involve binary search on answer.

»
19 месяцев назад, скрыть # |
 
Проголосовать: нравится +2 Проголосовать: не нравится

Tbh I still couldn't identify Binary search. My entire intuition of bs is "if it looks like dp, but constraints do not support dp and couldn't prove greedy then it must be binary search" and it works almost all the time.

»
19 месяцев назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

If some function is monotonic and you want to know the equal range of some value you should use binary search.

Why would you try to identify a problem is binary search or not? Just be ready to apply binary search whenever you noticed such a function.

»
19 месяцев назад, скрыть # |
 
Проголосовать: нравится +11 Проголосовать: не нравится

I apply binary search when i notice a pattern of TT..TF..FF or F..FTT..T .

»
19 месяцев назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

One trick you can find in some problems is trying to find a number (often the minimum or the maximum) that works for a certain dataset, and you can verify if a certain number works in O(1) or O(n) (or different depending on the problem and input size).

I'll use this leetcode problem as an example.

You can verify if a certain amount of seconds works with an O(n) operation, and since the function is monotonic, you can simply search for the minimum value that works and return that.

(this doesn't apply to all problems, but it should hopefully be helpful)

»
19 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Binary Search works when a function $$$f$$$ is increasing/decreasing (sorted in increasing/decreasing way) on $$$\mathbb{R}$$$ in general case , so if you've a computational complexity of $$$\mathcal{O}(f')$$$ for $$$f$$$ then you can apply binary search on $$$\mathcal{O}(f' \times \log(|r-l|)$$$ that's we apply binary search on range $$$[l,r]$$$.

However in some case this defintion can be optimized by some optimizations such as precalculation , for example :

Given an array $$$a$$$ of $$$a_1,a_2,..,a_n$$$ , find any subarray of sum $$$x$$$

Spoiler

Example 2