[LeetCode Daily Question] - 1365. How many numbers are smaller than the current number

One [topic category]

  • to sort

Two [question difficulty]

  • Simple

Three [topic number]

  • 1365. How many numbers are smaller than the current number

Four [title description]

  • Given an array nums, for each element nums[i], please count the number of all numbers smaller than it in the array.
  • In other words, for each nums[i] you must compute the number of valid js such that j != i and nums[j] < nums[i].
  • Return the answer as an array.

Five [topic example]

  • Example 1:

    • Input: nums = [8,1,2,2,3]
    • Output: [4,0,1,1,3]
    • explain:
      • For nums[0]=8 there are four smaller numbers: (1, 2, 2 and 3).
      • For nums[1]=1 there is no number smaller than it.
      • For nums[2]=2 there exists a smaller number: (1).
      • For nums[3]=2 there exists a smaller number: (1).
      • For nums[4]=3 there are three smaller numbers: (1, 2 and 2).
  • Example 2:

    • Input: nums = [6,5,4,8]
    • Output: [2,1,0,3]
  • Example 3:

    • Input: nums = [7,7,7,7]
    • output: [0,0,0,0]

Six [topic prompt]

  • 2 < = n u m s . l e n g t h < = 500 2 <= nums.length <= 500 2<=nums.length<=500
  • 0 < = n u m s [ i ] < = 100 0 <= nums[i] <= 100 0<=nums[i]<=100

Seven [problem-solving ideas]

  • Use the idea of ​​counting sort to solve this problem
  • First use the count array to save the number of occurrences of each number
  • Then use the count array again to calculate the sum of the number less than the current number and the number of the current number
  • For the i-th number, the value of count[i-1] is the number of numbers less than the i-th number
  • It is necessary to pay attention to the special treatment of the number 0, otherwise a negative index subscript will appear, resulting in an error
  • Finally return the result

Eight 【Time Frequency】

  • Time complexity: O ( n + k ) O(n+k)O ( n+k) n n n is the length of the incoming array,kkk is the value field size of the incoming array
  • Space complexity: O ( k ) O(k)O ( k )kkk is the value field size of the incoming array

Nine [code implementation]

  1. Java language version
class Solution {
    
    
    public int[] smallerNumbersThanCurrent(int[] nums) {
    
    
        int[] count = new int[101];
        int n = nums.length;
        for(int i = 0; i < n;i++){
    
    
            count[nums[i]]++;
        }
        for(int i = 1; i < 101;i++){
    
    
            count[i] += count[i - 1];
        }
        int[] res = new int[n];
        for(int i = 0;i < n;i++){
    
    
            res[i] = nums[i] == 0 ? 0 : count[nums[i] - 1];
        }
        return res;
    }
}
  1. C language version
int* smallerNumbersThanCurrent(int* nums, int numsSize, int* returnSize)
{
    
    
    int* count = (int*)calloc(101, sizeof(int));
    int n = numsSize;
    for(int i = 0;i < n;i++)
    {
    
    
        count[nums[i]]++;
    }
    for(int i = 1;i < 101;i++)
    {
    
    
        count[i] += count[i - 1];
    }
    int* res = (int*)calloc(n, sizeof(int));
    for(int i = 0;i < n;i++)
    {
    
    
        res[i] = nums[i] == 0 ? 0 : count[nums[i] - 1];
    }
    *returnSize = n;
    return res;
}
  1. Python language version
class Solution:
    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
        n = len(nums)
        count = [0] * 101
        for i in range(0, n):
            count[nums[i]] += 1
        for i in range(1, 101):
            count[i] += count[i - 1]
        res = [0] * n
        for i in range(0, n):
            res[i] = 0 if nums[i] == 0 else count[nums[i] - 1]
        return res
  1. C++ language version
class Solution {
    
    
public:
    vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
    
    
        vector<int> count(101, 0);
        int n = nums.size();
        for(int i = 0;i < n;i++){
    
    
            count[nums[i]]++;
        } 
        for(int i = 1; i < 101;i++){
    
    
            count[i] += count[i - 1];
        }
        vector<int> res(n, 0);
        for(int i = 0;i < n;i++){
    
    
            res[i] = nums[i] == 0 ? 0 : count[nums[i] - 1];
        }
        return res;
    }
};

Ten【Submission results】

  1. Java language version
    insert image description here

  2. C language version
    insert image description here

  3. Python language version
    insert image description here

  4. C++ language version
    insert image description here

Guess you like

Origin blog.csdn.net/IronmanJay/article/details/132556406