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.
Example 1:
Example 2:
Example 3:
Constraints:
2 <= nums.length <= 500
0 <= nums[i] <= 100
对数组中每个数统计有多少数小于它,元素取值范围[0, 100]。由于取值范围有限,因此可以统计频数再从头到尾累加遍历一次可得小于等于该数的出现次数,放在数组cnt中。返回时取cnt[nums[i] - 1]就是小于nums[i]所出现的次数和。
Last updated
Was this helpful?