'''

    Given a positive integer num, write a function which returns True if num is a perfect square else False.

    Link: https://leetcode.com/problems/valid-perfect-square/?tab=Description

    Example:

        Example 1:
            Input: 16
            Returns: True

        Example 2:
            Input: 14
            Returns: False

    Solution:

        Use binary search to search between 1 to N.
            Similar to: https://discuss.leetcode.com/topic/78605/python-solution-with-detailed-explanation

        Just be careful of boundary conditions. For example, test case num = 1, num = 0 should be tested by hand.

    Source: None

'''

class Solution(object):

    def isPerfectSquare(self, num):
        """
        :type num: int
        :rtype: bool
        """
        low, high = 1, num
        while low <= high:
            mid = low + (high-low)//2
            sq = mid*mid
            if sq == num:
                return True
            elif sq > num:
                high = mid - 1
            else:
                low = mid + 1
        return False