Skip to main content

315 - Count of Smaller Numbers After Self

Details

KeyValue
Linkhttps://leetcode.com/problems/count-of-smaller-numbers-after-self/
LanguagePython 3
Runtime2466 ms, faster than 92.95% of Python3 online submissions for Count of Smaller Numbers After Self
Memory Usage35.1 MB, less than 49.01% of Python3 online submissions for Count of Smaller Numbers After Self
DatastructuresList[int]
AlgorithmsBinary Search
ComplexityTime: O(NlogN) Memory: O(N)

Procedure

  1. ...

Code

Option 1: Use SortedList

from sortedcontainers import SortedList

class Solution:
def countSmaller(self, nums: List[int]) -> List[int]:
result, sortedList = deque(), SortedList()
for num in nums[::-1]:
result.appendleft(sortedList.bisect_left(num))
sortedList.add(num)
return result

Option 2

class Solution:
def countSmaller(self, nums: List[int]) -> List[int]:
for num in nums:
i = bisect_left(sorted_nums,num)
result.append(i)
del sorted_nums[i]
return result