Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d060230

Browse files
Leetcode 1365 solution
0 parents commit d060230

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Link to question: https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/
2+
def smallerNumbersThanCurrent(nums):
3+
count = [0]*101
4+
5+
for num in nums:
6+
count[num] += 1
7+
8+
# Maintain a running sum in the count array
9+
for i in range(1, 101):
10+
count[i] += count[i-1]
11+
12+
final_ans = [0] * len(nums)
13+
14+
# Format to fit output answer
15+
for i in range(len(nums)):
16+
curr_num = nums[i]
17+
# If the number is 0, there will never be elements less than it
18+
if curr_num == 0:
19+
final_ans[i] = 0
20+
else:
21+
# Set it to the running sum up to the previous element in nums
22+
final_ans[i] = count[curr_num-1]
23+
24+
return final_ans

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /