LeetCode 1365. How Many Numbers Are Smaller Than the Current Number

[Easy] LeetCode 1365. How Many Numbers Are Smaller Than the Current Number

链接:https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/

题目描述

Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j’s such that j != i and nums[j] < nums[i].

Return the answer in an array.

给你一个数组 nums,对于其中每个元素 nums[i],请你统计数组中比它小的所有数字的数目。
换而言之,对于每个 nums[i] 你必须计算出有效的 j 的数量,其中 j 满足 j != i 且 nums[j] < nums[i] 。

以数组形式返回答案。

Example 1:

Input: nums = [8,1,2,2,3]
Output: [4,0,1,1,3]
Explanation:
For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).
For nums[1]=1 does not exist any smaller number than it.
For nums[2]=2 there exist one smaller number than it (1).
For nums[3]=2 there exist one smaller number than it (1).
For nums[4]=3 there exist three smaller numbers than it (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]


Tag: Array
解题思路
这道题目让我们计算数组中每一个数字对应比他小的数字有多少。最简单的办法就是用一个二维数组,数组中每一位的第一个是这个数字本身,第二个是这个数字在数组中的位置。然后对二维数组的第一位进行排列,这样可以获得一个从小到大排列好的数组,然后根据排列好的数组中找到原数组对应元素的位置,将计算出比他小的元素个数放置在原数组位置上(复用原数组)。最后返回。时间复杂度是O(nlogn)因为要排序。

解法一:

class Solution {
    public int[] smallerNumbersThanCurrent(int[] nums) {
        
        int[][] copy = new int[nums.length][2];
        for(int i=0; i< nums.length; i++){
            copy[i] = new int[]{nums[i], i};    
        }
        Arrays.sort(copy, (a, b) -> a[0]-b[0]);
        int count =0;
        for(int i=0; i<copy.length; i++){
        	nums[copy[i][1]] = count;
            if(i < copy.length-1 && copy[i][0] != copy[i+1][0]){
                count = i+1;
            }
        }
        return nums;
    }
}

解法二:
这道题目还有一个投机取巧的地方,题目当中给出来整数都是在0到100范围内的,所以我们可以根据这个信息建立一个长度为101的array用来存放对应元素所在的坐标。然后遍历一遍新的数组,计算每一个位置元素的个数并且累加。最后求得答案

class Solution {
    public int[] smallerNumbersThanCurrent(int[] nums) {
        List<Integer>[] numToPos = new ArrayList[101];
        
        for(int i=0; i< nums.length; i++){
            if(numToPos[nums[i]] == null) numToPos[nums[i]] = new ArrayList<>();
            numToPos[nums[i]].add(i);
        } 

        int count =0;
        for(int i=0; i< numToPos.length; i++){
            if(numToPos[i] != null){
                for(int pos: numToPos[i]){
                    nums[pos] = count;
                }
                count+=numToPos[i].size();
            }
        }
        
        return nums;
    }
}

解法三:
或者这样也可以

class Solution {
    public int[] smallerNumbersThanCurrent(int[] nums) {
        int[] count = new int[101];
        int[] res = new int[nums.length];
        
        for (int i =0; i < nums.length; i++) {
            count[nums[i]]++;
        }
        
        for (int i = 1 ; i <= 100; i++) {
            count[i] += count[i-1];    
        }
        
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0)
                res[i] = 0;
            else 
                res[i] = count[nums[i] - 1];
        }
        
        return res;        
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值