""" 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()
""" 0766.1 - Toeplitz Matrix - Solution 1 - Compare with Top-Left Neighbor """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:
"""Toeplitz Matrix Function"""
m, n = len(matrix), len(matrix[0])
for i in range(1, m):
for j in range(1, n):
if matrix[i][j] != matrix[i - 1][j - 1]:
return False
return True
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().isToeplitzMatrix([[1,2,3,4],[5,1,2,3],[9,5,1,2]])) # True
print(Solution().isToeplitzMatrix([[1,2],[2,2]])) # False
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0766.2 - Toeplitz Matrix - Solution 2 - Check Each Diagonal Explicitly """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:
"""Toeplitz Matrix Function"""
m, n = len(matrix), len(matrix[0])
def check_diagonal(r: int, c: int) -> bool:
"""Check all elements on the diagonal starting at (r, c)"""
val = matrix[r][c]
while r < m and c < n:
if matrix[r][c] != val:
return False
r += 1
c += 1
return True
# Check diagonals starting from the first column
for i in range(m):
if not check_diagonal(i, 0):
return False
# Check diagonals starting from the first row (skip (0,0) already checked)
for j in range(1, n):
if not check_diagonal(0, j):
return False
return True
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().isToeplitzMatrix([[1,2,3,4],[5,1,2,3],[9,5,1,2]])) # True
print(Solution().isToeplitzMatrix([[1,2],[2,2]])) # False
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()