【算法】1365. 有多少小于当前数字的数字(多语言实现)

125 阅读1分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第2天,点击查看活动详情


1365. 有多少小于当前数字的数字:

给你一个数组 nums,对于其中每个元素 nums[i],请你统计数组中比它小的所有数字的数目。

换而言之,对于每个 nums[i] 你必须计算出有效的 j 的数量,其中 j 满足 j != i nums[j] < nums[i]

以数组形式返回答案。

样例 1:

输入:
	nums = [8,1,2,2,3]
	
输出:
	[4,0,1,1,3]
	
解释: 
	对于 nums[0]=8 存在四个比它小的数字:(1,2,2 和 3)。 
	对于 nums[1]=1 不存在比它小的数字。
	对于 nums[2]=2 存在一个比它小的数字:(1)。 
	对于 nums[3]=2 存在一个比它小的数字:(1)。 
	对于 nums[4]=3 存在三个比它小的数字:(1,2 和 2)。

样例 2:

输入:
	nums = [6,5,4,8]
	
输出:
	[2,1,0,3]

样例 3:

输入:
	nums = [7,7,7,7]
	
输出:
	[0,0,0,0]

提示:

  • 2 <= nums.length <= 500
  • 0 <= nums[i] <= 100

分析

  • 面对这道算法题目,我陷入了沉思。
  • 如果直接暴力硬解,那需要每个数字都遍历整个数组,时间复杂度O(n2n^2),太慢了。
  • 提示中已经给了数组中数字的范围是 [0, 100] 。我们可以直接先统计出每个数字的个数,然后再统计出小于等于每个数字的个数,最后再遍历数组利用之前的统计返回结果。
  • 注意 [0, 100] 一共是101个数。

题解

java

class Solution {
    public int[] smallerNumbersThanCurrent(int[] nums) {
        int[] counter = new int[101];
        for (int num : nums) {
            counter[num]++;
        }
        for (int i = 1; i <= 100; ++i) {
            counter[i] += counter[i - 1];
        }

        int n = nums.length;
        int[] ans = new int[n];
        for (int i = 0; i < n; ++i) {
            if (nums[i] > 0) {
                ans[i] = counter[nums[i] - 1];
            }
        }

        return ans;
    }
}

c

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* smallerNumbersThanCurrent(int* nums, int numsSize, int* returnSize){
    int counter[101];
    memset(counter, 0, sizeof(counter));
    for (int i = 0; i < numsSize; ++i) {
        counter[nums[i]]++;
    }
    for (int i = 1; i <= 100; ++i) {
        counter[i] += counter[i - 1];
    }

    *returnSize = numsSize;
    int *ans = malloc(numsSize * sizeof(int));
    for (int i = 0; i < numsSize; ++i) {
        ans[i] = nums[i] == 0 ? 0 : counter[nums[i] - 1];
    }

    return ans;
}

c++

class Solution {
public:
    vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
        vector<int> counter(101, 0);
        int n = nums.size();
        for (int &num: nums) {
            counter[num]++;
        }
        for (int i = 1; i <= 100; ++i) {
            counter[i] += counter[i - 1];
        }

        vector<int> ans;
        for (int &num: nums) {
            ans.push_back(num == 0 ? 0 : counter[num - 1]);
        }

        return ans;
    }
};

python

class Solution:
    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
        counter = [0] * 101
        for num in nums:
            counter[num] += 1
        for i in range(1, 101):
            counter[i] += counter[i - 1]

        ans = []
        for num in nums:
            ans.append(0 if num == 0 else counter[num - 1])

        return ans
        

go

func smallerNumbersThanCurrent(nums []int) []int {
    counter := [101]int{}
	for _, num := range nums {
            counter[num]++
	}
	for i := 1; i <= 100; i++ {
            counter[i] += counter[i-1]
	}

	ans := make([]int, len(nums))
	for i, num := range nums {
            if num > 0 {
                ans[i] = counter[num-1]
            }
	}

	return ans
}

rust

impl Solution {
    pub fn smaller_numbers_than_current(nums: Vec<i32>) -> Vec<i32> {
        let mut counter = vec![0; 101];
        nums.iter().for_each(|&num| {
            counter[num as usize] += 1;
        });
        (1..101).for_each(|i| {
            counter[i] += counter[i - 1];
        });

        nums.iter().map(|&num| {
            if num == 0 {
                0
            } else {
                counter[num as usize - 1]
            }
        }).collect()
    }
}

在这里插入图片描述


原题传送门:https://leetcode-cn.com/problems/how-many-numbers-are-smaller-than-the-current-number/


非常感谢你阅读本文~
放弃不难,但坚持一定很酷~
希望我们大家都能每天进步一点点~
本文由 二当家的白帽子:https://juejin.cn/user/2771185768884824/posts 博客原创~