Hyoseo Lee
01 Index 02 Current page Writing 03 Work 04 Notes 05 Games 06 Author
leetcode

367. Valid Perfect Square

Topic Binary Search
Area Algorithms
Summary
just compare the num with all the perfect squares from 1 to max.

Problem

View on LeetCode →

Difficulty: Easy
Tags: Math, Binary Search

Intuition

this one seemed easy. i can just compare the number with all the perfect squres and finds it. it will only take sqrt(n) time.

Approach

just compare the num with all the perfect squares from 1 to max.

Solution

bool isPerfectSquare(int num) {
    int i, k;
    i = 1;
    k = 1;
    while(true){
        if(num == k){
            return true;
        }
        i++;
        if(i > 46340){
            return false;
        }
        k = i*i;
        if(k > num){
            return false;
        }
    }
}

Complexity

  • Time: O(n)O(\sqrt n)

  • Space: O(1)O(1)

Thoughts

this one was easy.