""" 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()
""" 0997.1 - Find the Town Judge - Solution 1 - Trust Count Array """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def findJudge(self, n: int, trust: List[List[int]]) -> int:
"""Find the Town Judge Function"""
count = [0] * (n + 1)
for a, b in trust:
count[a] -= 1
count[b] += 1
for i in range(1, n + 1):
if count[i] == n - 1:
return i
return -1
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().findJudge(2, [[1, 2]])) # Expected: 2
print(Solution().findJudge(3, [[1, 3], [2, 3]])) # Expected: 3
print(Solution().findJudge(3, [[1, 3], [2, 3], [3, 1]])) # Expected: -1
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0997.2 - Find the Town Judge - Solution 2 - Two Sets """
#####################################################################################
# Imports
#####################################################################################
from typing import List
from collections import defaultdict
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def findJudge(self, n: int, trust: List[List[int]]) -> int:
"""Find the Town Judge Function"""
trusts_someone = set()
trusted_by = defaultdict(int)
for a, b in trust:
trusts_someone.add(a)
trusted_by[b] += 1
for i in range(1, n + 1):
if i not in trusts_someone and trusted_by[i] == n - 1:
return i
return -1
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().findJudge(2, [[1, 2]])) # Expected: 2
print(Solution().findJudge(3, [[1, 3], [2, 3]])) # Expected: 3
print(Solution().findJudge(3, [[1, 3], [2, 3], [3, 1]])) # Expected: -1
print(Solution().findJudge(1, [])) # Expected: 1
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()