Problem solving/Algorithms

[LeetCode] 1365. How Many Numbers Are Smaller Than the Current Number (Python)

Young_A 2021. 1. 10. 09:32

๋ชฉ์ฐจ

    LeetCode - Problems - Algorithms - 1365. How Many Numbers Are Smaller Than the Current Number

    Problem Description

    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:

    Constraints:

    My Solution (Python)

    class Solution(object):
        def smallerNumbersThanCurrent(self, nums):
            """
            :type nums: List[int]
            :rtype: List[int]
            """
            answer = []
    
            for i in range(len(nums)):
                counter = 0
                for j in range(len(nums)):
                    if i == j:
                        continue
                    else:
                        if nums[i] > nums[j]:
                            counter += 1
                answer.append(counter)
            
            return answer