1365. How Many Numbers Are Smaller Than the Current Number

class Solution:
def smallerNumbersThanCurrent(self, nums):
hashtable=dict()
for i in nums:
hashtable[i]=0
for i in nums:
for j in hashtable:
if(j>i):
hashtable[j]+=1
for i in range(len(nums)):
nums[i]=hashtable[nums[i]]
return(nums)
#prefix sum of the counts array usedclass Solution:
def smallerNumbersThanCurrent(self, nums):
max_=max(nums)
counts = [0]*(max_+1)
for i in range(len(nums)):
counts[nums[i]]+=1
for i in range(1,max_+1):
counts[i]+=counts[i-1]
for i in range(len(nums)):
if(nums[i]==0):
nums[i]=0
else:
nums[i]=counts[nums[i]-1]
return nums
#Time O(n)
#Space O(1) to save the space we store the final answer in to the same array

--

--

--

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

CTF Challenge n3ph4ck Writeup

Jenkins: Build Triggers

An Introduction to Advanced Loops in Python

Understanding Async, Avoiding Deadlocks in C#

Numpy — 02

image.png

Why is On-Demand OpenStack Important for SMBs?

My first 1.5 year learning web development, and what I’ve learnt

GitHub Copilot Pair Programming: Something Old, Something New

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dhanarajappu

Dhanarajappu

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.

More from Medium

How is DeFi transforming the finance ecosystem?

Know-How Telephony Integrated with Zoho CRM

Performing Smart Contract Audit : Why And How

Launching Into The Deep