""" 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()
""" 0703.1 - Kth Largest Element in a Stream - Solution 1 - Sorting Approach """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class KthLargest:
"""Kth Largest Stream Class"""
def __init__(self, k: int, nums: List[int]):
"""Initialize with k and initial stream"""
self.k = k
self.nums = sorted(nums)
def add(self, val: int) -> int:
"""Add value and return kth largest"""
self.nums.append(val)
self.nums.sort()
return self.nums[-self.k]
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
kth = KthLargest(3, [4, 5, 8, 2])
print(kth.add(3)) # 4
print(kth.add(5)) # 5
print(kth.add(10)) # 5
print(kth.add(9)) # 8
print(kth.add(4)) # 8
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0703.2 - Kth Largest Element in a Stream - Solution 2 - Min Heap Approach """
#####################################################################################
# Imports
#####################################################################################
import heapq
from typing import List
#####################################################################################
# Classes
#####################################################################################
class KthLargest:
"""Kth Largest Stream Class"""
def __init__(self, k: int, nums: List[int]):
"""Initialize with k and initial stream using a min heap"""
self.k = k
self.heap = nums
heapq.heapify(self.heap)
while len(self.heap) > k:
heapq.heappop(self.heap)
def add(self, val: int) -> int:
"""Add value and return kth largest"""
heapq.heappush(self.heap, val)
if len(self.heap) > self.k:
heapq.heappop(self.heap)
return self.heap[0]
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
kth = KthLargest(3, [4, 5, 8, 2])
print(kth.add(3)) # 4
print(kth.add(5)) # 5
print(kth.add(10)) # 5
print(kth.add(9)) # 8
print(kth.add(4)) # 8
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()