அல்கோரிதம்variations

Binary Search மாறுபாடுகள்

TT
Testlaa Team
May 14, 20261 min read

Doubles-இல் real binary search, integers-இல் discrete. முதலில் integer templates முழுமை.

Try this in Python

def sqrt_int_floor(n: int) -> int:
    lo, hi = 0, n
    while lo < hi:
        mid = (lo + hi + 1) // 2
        if mid * mid <= n:
            lo = mid
        else:
            hi = mid - 1
    return lo


print(sqrt_int_floor(15))

Key takeaways

  • Integer: overflow (mid <= n // mid).
  • Double: iterations/epsilon.
  • Logic ஒன்று, type மாறும்.

Tags:

Binary searchPythonமாணவர்கள்