""" 0001.1 - Solution 1 - Brute Force Approach """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def twoSum(self, nums: List[int], target: int) -> List[int]:
"""Two Sum Function"""
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target:
return [i, j]
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().twoSum([3, 3], 6))
print(Solution().twoSum([3, 2, 4], 6))
print(Solution().twoSum([2, 7, 11, 15], 9))
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0001.1 - Solution 1 - Using Enumerate """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def twoSum(self, nums: List[int], target: int) -> List[int]:
"""Two Sum Function"""
for key1, num1 in enumerate(nums):
for key2, num2 in enumerate(nums[key1 + 1 :], key1 + 1):
# enumerate(iterable, start=1) -> start value determines starting index
# Printing value and key will output 1 i , 2 j, 3 k, 4 l
if num1 + num2 == target:
return [key1, key2]
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().twoSum([3, 3], 6))
print(Solution().twoSum([3, 2, 4], 6))
print(Solution().twoSum([2, 7, 11, 15], 9))
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 1365.1 - Solution 1 - Brute Force """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
"""How Many Numbers Are Smaller Than the Current Number"""
result = []
for i in range(len(nums)):
count = 0
for j in range(len(nums)):
if j != i and nums[j] < nums[i]:
count += 1
result.append(count)
return result
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().smallerNumbersThanCurrent([8, 1, 2, 2, 3])) # [4, 0, 1, 1, 3]
print(Solution().smallerNumbersThanCurrent([6, 5, 4, 8])) # [2, 1, 0, 3]
print(Solution().smallerNumbersThanCurrent([7, 7, 7, 7])) # [0, 0, 0, 0]
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 1365.2 - Solution 2 - Sorting with Index Mapping """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
"""How Many Numbers Are Smaller Than the Current Number"""
sorted_nums = sorted(nums)
mapping = {}
for i, num in enumerate(sorted_nums):
if num not in mapping:
mapping[num] = i
return [mapping[num] for num in nums]
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().smallerNumbersThanCurrent([8, 1, 2, 2, 3])) # [4, 0, 1, 1, 3]
print(Solution().smallerNumbersThanCurrent([6, 5, 4, 8])) # [2, 1, 0, 3]
print(Solution().smallerNumbersThanCurrent([7, 7, 7, 7])) # [0, 0, 0, 0]
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 1365.3 - Solution 3 - Counting Sort """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
"""How Many Numbers Are Smaller Than the Current Number"""
count = [0] * 101
for num in nums:
count[num] += 1
# Build prefix sum: count[i] = number of elements < i
prefix = [0] * 101
for i in range(1, 101):
prefix[i] = prefix[i - 1] + count[i - 1]
return [prefix[num] for num in nums]
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().smallerNumbersThanCurrent([8, 1, 2, 2, 3])) # [4, 0, 1, 1, 3]
print(Solution().smallerNumbersThanCurrent([6, 5, 4, 8])) # [2, 1, 0, 3]
print(Solution().smallerNumbersThanCurrent([7, 7, 7, 7])) # [0, 0, 0, 0]
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()