简体   繁体   中英

time complexity of this algorithm written below

According to my calculations, the time complexity of this algorithm/code is O(logN) as it is an enhancement of binary search, but while submitting the code in leetcode and other platforms, it is said that the time limit is exceeded. they are also expecting the time complexity of this algorithm to be O(logN), so please confirm is the complexity of code written below is O(logN) or is it different from that?

class Solution {
public:
    int findLast(vector<int> arr, int n, int x)
    {
        int l = 0;
        int h = n - 1;
        int mid;
        while (h >= l) {
            mid = (l + h) / 2;
            if (arr[mid] == x) {
                if (arr[mid + 1] != x) {
                    return mid;
                }
                else {
                    l = mid + 1;
                }
            }
            else if (arr[mid] > x) {
                h = mid;
            }
            else if (arr[mid] < x) {
                l = mid + 1;
            }
        }
        return -1;
    }

    int findFirst(vector<int> arr, int n, int x)
    {
        int l = 0;
        int h = n - 1;
        int mid;
        while (h >= l) {
            mid = (l + h) / 2;
            if (arr[mid] == x) {
                if (arr[mid - 1] != x) {
                    return mid;
                }
                else {
                    h = mid;
                }
            }
            else if (arr[mid] > x) {
                h = mid;
            }
            else if (arr[mid] < x) {
                l = mid + 1;
            }
        }
        return -1;
    }

    vector<int> searchRange(vector<int>& nums, int target)
    {
        int last = findLast(nums, nums.size(), target);
        int first = findFirst(nums, nums.size(), target);
        vector<int> v1 = { first, last };
        return v1;
    }
};

Your code's time complexity is O(∞) because it can perform an infinite loop.

Consider what happens when you run findLast on a one-element vector. You will have h , l , and mid equal to 0. If you enter the arr[mid] > x branch then you run h = mid; which leaves the variables unchanged, and repeats indefinitely.

Besides this, you should take the vector parameter by reference to avoid a copy which would make this a linear runtime.

Your code does not handle some corner cases where the input could be [1,1] and the target is 1 . In findLast , the low and high variables would come to 1 and segmentation fault would occur when you check for arr[mid+1] . The same explanation would go for findFirst as well. Try this and see if it helps:

int findLast(vector<int> arr, int n, int x)
{
    int l = 0;
    int h = n - 1;
    int mid;
    while (h > l)
    {
        mid = (l + h) / 2;
        if (arr[mid] == x)
        {
            if (arr[mid + 1] != x)
            {
                return mid;
            }
            else
            {
                l = mid + 1;
            }
        }
        else if (arr[mid] > x)
        {
            h = mid;
        }
        else if (arr[mid] < x)
        {
            l = mid + 1;
        }
    }
    return arr[l] == x ? l : -1;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM