How Many Numbers Are Smaller Than the Current Number Leetcode Solution


Ниво тешкоће Лако
Често питани у адобе амазонка Блоомберг
Ред Хасхинг

Изјава о проблему

In this problem, we are given an поредак. For each element of this array, we have to find out the number of elements smaller than that element.
i.e. for each i (XNUMX<=i<arr.length) we have to find out count of elements less than the number arr[i]. For that we have to return an answer array.

Пример

nums = [8,1,2,2,3]
[4,0,1,1,3]

objašnjenje:
For nums[XNUMX]=XNUMX there exist four smaller numbers than it (XNUMX, XNUMX, XNUMX and XNUMX).
For nums[XNUMX]=XNUMX does not exist any smaller number than it.
For nums[XNUMX]=XNUMX there exist one smaller number than it (XNUMX).
For nums[XNUMX]=XNUMX there exist one smaller number than it (XNUMX).
For nums[XNUMX]=XNUMX there exist three smaller numbers than it (XNUMX, XNUMX and XNUMX).

nums = [6,5,4,8]
[2,1,0,3]

Приступ (груба сила)

We can simply run a loop for every element, starting from left to right.
For each element, we will find the count of the elements which are lesser than the current number.
So basically, we run one outer loop for each element and in inner loop, we will traverse the whole array to count every element less than current value.

Implementation for How Many Numbers Are Smaller Than the Current Number Leetcode Solution

Програм Ц ++

#include <bits/stdc++.h>
using namespace std;

vector<int> smallerNumbersThanCurrent(vector<int>& nums) 
{
    vector<int> ans;

    for(int i=0;i<nums.size();i++)
    {
        int val=nums.at(i),count=0;
        for(int j=0;j<nums.size();j++)
        {
            if(nums.at(j)<val)  count++;
        }
        ans.push_back(count);
    }

    return ans;
}

int main() 
{
    vector<int> nums{ 8 , 1, 2 , 2 , 3 }; 
    vector<int> ans=smallerNumbersThanCurrent(nums);
    
    for(auto val:ans) cout<<val<<" ";
    cout<<endl;
    
    return 0;  
}
4 0 1 1 3

Јава Програм

import java.lang.*;

class Rextester
{  
    public static int[] smallerNumbersThanCurrent(int[] nums) 
    {
        int[]ans=new int[nums.length];

        for(int i=0;i<nums.length;i++)
        {
            int count=0;
            for(int j=0;j<nums.length;j++)
            {
                if(nums[i]>nums[j])   count++;
            }
            ans[i]=count;
        }

        return ans;
    }
    
    public static void main(String args[])
    {
       int[]nums={8,1,2,2,3};  
       int[]ans=smallerNumbersThanCurrent(nums);
        
       for(int num:ans)  
       {
           System.out.print(num+" ");
       }
        
    }
}
4 0 1 1 3

Complexity Analysis for How Many Numbers Are Smaller Than the Current Number Leetcode Solution

Сложеност времена

O(n^XNUMX) : We are using nested loop each running for n lengths. Hence time complexity will be O(n^XNUMX).

Сложеност простора 

О (1): We have just used an array of length n to return the answer. Beside this we have not used any extra memory. So space complexity is constant.

Приступ (оптимизован)

As we know that number smaller than any element in an array is equal to count of number less than or equal to that number-XNUMX.
Like, in array
КСНУМКС КСНУМКС КСНУМКС КСНУМКС КСНУМКС КСНУМКС
count of numbers less than XNUMX is count of numbers less than or equal to XNUMX.
So, if we somehow store the count of every element present in the given array, then we can easily say that how many elements are less than it.

How Many Numbers Are Smaller Than the Current Number Leetcode Solution

So, we are keeping a count array of size XNUMX. (because the number may be between [XNUMX])
every index of count array i will say the occurrence of number i in the given array.

Now, to know how many numbers are less than or equal to a number at a position, we have to apply prefix sum.
so, we will apply prefix sum on count array.

Then for each element i of the array, number of elements less than this number will be count[i-XNUMX].

So, in this approach firstly we will create our count array.
Then we will convert it into the prefix sum.
Then for each element num of input array we will have count[num-XNUMX].

Implementation for How Many Numbers Are Smaller Than the Current Number Leetcode Solution

Програм Ц ++

#include <bits/stdc++.h>
using namespace std;

vector<int> smallerNumbersThanCurrent(vector<int>& nums) 
{
    int count[101];
    memset(count,0,sizeof(count));
    for(auto& i:nums)
    {
        count[i]++;
    }

    for(int i=1;i<=100;i++)
    {
        count[i]=count[i-1]+count[i];
    }

    vector<int> ans;

    for(int i=0;i<nums.size();i++)
    {
        if(nums[i]==0)    ans.push_back(0);
        else    ans.push_back(count[nums[i]-1]);
    }

    return ans;
}

int main() 
{
    vector<int> nums{ 8 , 1, 2 , 2 , 3 }; 
    vector<int> ans=smallerNumbersThanCurrent(nums);
    
    for(auto val:ans) cout<<val<<" ";
    cout<<endl;
    
    return 0;  
}
4 0 1 1 3

Јава Програм

#include <bits/stdc++.h>
using namespace std;

vector<int> smallerNumbersThanCurrent(vector<int>& nums) 
{
    int count[101];
    memset(count,0,sizeof(count));
    for(auto& i:nums)
    {
        count[i]++;
    }

    for(int i=1;i<=100;i++)
    {
        count[i]=count[i-1]+count[i];
    }

    vector<int> ans;

    for(int i=0;i<nums.size();i++)
    {
        if(nums[i]==0)    ans.push_back(0);
        else    ans.push_back(count[nums[i]-1]);
    }

    return ans;
}

int main() 
{
    vector<int> nums{ 8 , 1, 2 , 2 , 3 }; 
    vector<int> ans=smallerNumbersThanCurrent(nums);
    
    for(auto val:ans) cout<<val<<" ";
    cout<<endl;
    
    return 0;  
}
4 0 1 1 3

Complexity Analysis for How Many Numbers Are Smaller Than the Current Number Leetcode Solution

Сложеност времена

O(max(n,XNUMX)): Time complexity will be O(Max(n,XNUMX)) where n is size of input array and XNUMX is taken because we are making extra array of size XNUMX which is also traversed.

Сложеност простора 

О (1): We are using an extra array of counts of size XNUMX, so space complexity constant.