Problem statement

https://binarysearch.com/problems/Check-if-Number-Is-Perfect-Square/

Solution

Equal to Leetcode 0367 Valid Perfect Square.

Complexity

It is O(log n) for time and O(1) for space.

Code

class Solution:
    def solve(self, num):
        beg, end = 0, num + 1
        while end - beg  > 1:
            mid = (beg + end)//2
            if mid*mid > num:
                end = mid
            else:
                beg = mid
                
        return beg*beg == num