sorted search increase performance

前端 未结 4 427
野性不改
野性不改 2021-01-29 14:53

Running through exercises on testdome...currently looking at https://www.testdome.com/for-developers/solve-question/9877

Implement function CountNum

相关标签:
4条回答
  • 2021-01-29 15:30

    This passed all 4 tests:

    public static int CountNumbers(int[] sortedArray, int lessThan)
    {
        int val = Array.BinarySearch(sortedArray, lessThan);
        return val < 0 ? ~val : val;
    }
    

    As others have said, they expect you to use Array.BinarySearch which I didn't realise until read the 2nd hint.

    0 讨论(0)
  • 2021-01-29 15:33

    You should have to break the foreach loop, once if condition get false because we already know given array is sorted so there is no point to keep on evaluating further elements. Please see below code snippet for reference.

    foreach (int i in sortedArray)
    {
        if (i < lessThan)
            returnedValued += 1;
        else break;
    }
    

    Please see below solution which passed all the 4 tests. I have used binary search technique to find out the element which is greater than lessThan variable.

    public static int CountNumbers(int[] sortedArray, int lessThan)
    {
        //Handle all the corner cases
        int legthOfArray = sortedArray.Length;
        if (legthOfArray == 0) return 0;
        if (sortedArray[0] >= lessThan) return 0;
        if (sortedArray[legthOfArray - 1] < lessThan) return legthOfArray;
        return FindIndexGreaterOrEqualIndex(sortedArray, legthOfArray, lessThan, legthOfArray / 2);
    }
    
    public static int FindIndexGreaterOrEqualIndex(int[] sortedArray, int lengthOfArray, int lessThan, int currentIndex)
    {
        while (true)
        {
            bool isCurrentElementLessThan = sortedArray[currentIndex] < lessThan;
            if (isCurrentElementLessThan) // Traverse Right hand side of binary tree.
                currentIndex = (int)Math.Ceiling((decimal)(currentIndex + lengthOfArray - 1) / 2);
            else if (sortedArray[currentIndex - 1] < lessThan && !isCurrentElementLessThan) //If array element is not less than and previous element is less than the given element. i.e. our answer so break the loop.
                break;
            else // Traverse Left hand side of binary tree.
                currentIndex = (int)Math.Ceiling((decimal)currentIndex / 2);
        }
        return currentIndex;
    }
    

    Have a look :)

    0 讨论(0)
  • 2021-01-29 15:43

    If you like to implement your own BST, you can try the following code. It is written in Python. You can rewrite it in other languages if required. It passes all the 4 tests.

    def count_numbers(sorted_list, less_than):
    
        if len(sorted_list) == 0 or sorted_list[0] >= less_than:
            return 0
        if sorted_list[-1] < less_than:
            return len(sorted_list)
    
        arr = sorted_list
        def lt_array(low, high, arr):
            mid = low + (high-low+1) //2 
            if arr[mid] >= less_than and arr[mid-1] < less_than:
                return mid
            if arr[mid] > less_than:
                return lt_array(low,mid,arr)
            else:
                return lt_array(mid, high, arr)
        
        return lt_array(0,len(sorted_list)-1, arr)
    
    0 讨论(0)
  • 2021-01-29 15:45

    They expect you to use the Array.BinarySearch method to get 100%.

    using System;
    
    public class SortedSearch
    {
        public static int CountNumbers(int[] sortedArray, int lessThan)
        {
            int lengthOfArray = sortedArray.Length;
            if (lengthOfArray == 0) return 0;
    
            if (sortedArray[0] >= lessThan) return 0;
            if (sortedArray[lengthOfArray - 1] < lessThan) return lengthOfArray;
    
            int index = Array.BinarySearch(sortedArray, lessThan);
            if (index < 0)
                return ~index;
    
            return index;
        }
    
        public static void Main(string[] args)
        {
            Console.WriteLine(SortedSearch.CountNumbers(new int[] { 1, 3, 5, 7 }, 4));
        }
    }
    
    0 讨论(0)
提交回复
热议问题