Lecode1365. 有多少小于当前数字的数字-20201026

题目描述

给你一个数组 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]
 
输入:nums = [7,7,7,7]
输出:[0,0,0,0]
 
提示:

  • 2 <= nums.length <= 500
  • 0 <= nums[i] <= 100
     
    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/how-many-numbers-are-smaller-than-the-current-number
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解题思路

方法1

题目要求计算:对于nums中每一个数,小于它的数有多少个。直接将数组从小到大排序得到sort_nums,然后按顺序取出原数组中的每个数,求该数在sort_nums中第一次出现位置的index。

代码

class Solution:
    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
        sort_nums = sorted(nums)
        ans = []
        for n in nums:
            ans.append(sort_nums.index(n))
        return ans

执行结果:通过
显示详情
执行用时:68 ms, 在所有 Python3 提交中击败了68.51%的用户
内存消耗:13.6 MB, 在所有 Python3 提交中击败了5.09%的用户
class Solution {
public:
    vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
        vector<int> ori_nums(nums);
        sort(nums.begin(), nums.end());
        int n = nums.size();
        vector<int> ans(n, 0);
        for (int i = 0; i < n; i++){
            ans[i] = &*find(nums.begin(), nums.end(), ori_nums[i]) - &nums[0];
        }
        return ans;
    }
};

执行结果:通过
显示详情
执行用时:36 ms, 在所有 C++ 提交中击败了49.10%的用户
内存消耗:10.2 MB, 在所有 C++ 提交中击败了11.74%的用户

复杂度

  • 时间复杂度: O ( N 2 ) O(N^2) O(N2), 快排 O ( N log ⁡ n ) O(N \log n) O(Nlogn),遍历求index O ( N 2 ) O(N^2) O(N2)
  • 空间复杂度: O ( N ) O(N) O(N),存储排序后的数组

方法2

可以在排序时顺带将下标也进行排序,这样就可以简化查找index。

代码

class Solution:
    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
        ln = len(nums)
        tmp = []
        for i, n in enumerate(nums):
            tmp.append([n, i])
        sort_nums = sorted(tmp, key = lambda x: x[0])
        ans = [0] * ln
        ind = 0
        for i in range(ln):
            if 0 == i:
                continue
            if sort_nums[i][0] != sort_nums[i-1][0]:
                ind = i
            ans[sort_nums[i][1]] = ind
        return ans

执行结果:通过
显示详情
执行用时:48 ms, 在所有 Python3 提交中击败了88.02%的用户
内存消耗:13.6 MB, 在所有 Python3 提交中击败了5.09%的用户
class Solution {
public:
    vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
        vector<pair<int, int>> nums_i;
        int ln = nums.size();
        for (int i = 0; i < ln; i++){
            nums_i.emplace_back(nums[i], i);
        }
        sort(nums_i.begin(), nums_i.end());

        vector<int> ans(ln, 0);
        int ind = -1;
        for (int i = 0; i < ln; i++){
            if (-1 == ind || nums_i[i].first != nums_i[i-1].first){
                ind = i;
            }
            ans[nums_i[i].second] = ind;
        }
        return ans;
    }
};

执行结果:通过
显示详情
执行用时:12 ms, 在所有 C++ 提交中击败了70.52%的用户
内存消耗:10.6 MB, 在所有 C++ 提交中击败了5.06%的用户

复杂度

  • 时间复杂度: O ( N log ⁡ N ) O(N\log N) O(NlogN), 快排 O ( N log ⁡ N ) O(N \log N) O(NlogN),遍历求index O ( N 2 ) O(N^2) O(N2)
  • 空间复杂度: O ( N ) O(N) O(N),需新开辟数组。

方法3

题目说明数组元素的值域为[0, 100],可建立一个频次数组 c n t cnt cnt c n t [ i ] cnt[i] cnt[i] 表示数字 i i i 出现的次数。这样对于数字 i i i 而言,小于它的数目就为 sum( c n t [ 0 : i ] cnt[0:i] cnt[0:i]) 不包括 i i i
参考自LeeCode解答

代码

class Solution:
   def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
       cnt = [0] * 101
       for n in nums:
           cnt[n] += 1
       ans = []
       for n in nums:
           ans.append(sum(cnt[:n]))
       return ans

执行结果:通过
显示详情
执行用时:64 ms, 在所有 Python3 提交中击败了72.58%的用户
内存消耗:13.6 MB, 在所有 Python3 提交中击败了5.09%的用户
class Solution {
public:
    vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
        int ln = nums.size();
        vector<int> cnt(101, 0);
        for (int n: nums){
            cnt[n]++;
        }
        for (int i = 1; i < 101; i++){
            cnt[i] += cnt[i-1];
        }
        vector<int> ans(ln, 0);
        for (int i = 0; i < ln; i++){
            ans[i] = 0 == nums[i] ? 0 : cnt[nums[i] - 1];
        }
        return ans;
    }
};

执行结果:通过
显示详情
执行用时:4 ms, 在所有 C++ 提交中击败了98.00%的用户
内存消耗:10.2 MB, 在所有 C++ 提交中击败了8.73%的用户

复杂度

  • 时间复杂度: O ( N + K ) O(N + K) O(N+K), 其中K为值域大小。需要遍历两次原数组和一次频次数组 c n t cnt cnt.
  • 空间复杂度: O ( K ) O(K) O(K),需新开辟频次数组 c n t cnt cnt
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

silenceagle

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值