[Leet Code] How Many Numbers Are Smaller Than The Current Number

Matthew Boyd
1 min readNov 24, 2020

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

Explanation of the problem:

You are given an array of numbers, and for each number, you have to find out how many numbers are smaller than that given number in the array.

Solution:

class Solution(object):
def smallerNumbersThanCurrent(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
answer = []
count = 0

for i in range(len(nums)):
for j in range(len(nums)):
if nums[j] < nums[i]:
count = count + 1
answer.append(count)
count = 0

return answer

Due to having to loop over the array twice, we will need two for loops.

for i in range(len(nums)):
for j in range(len(nums)):

Then it is fairly simple logic of: if the inner loop of values is less than the number specified, then we increment the counter by 1.

Then we just return the answer!

--

--