""" 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()
""" 1351.1 - Count Negative Numbers in a Sorted Matrix - Solution 1 - Brute Force """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def countNegatives(self, grid: List[List[int]]) -> int:
"""Count Negatives Function"""
count = 0
for row in grid:
for num in row:
if num < 0:
count += 1
return count
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().countNegatives([[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]])) # 8
print(Solution().countNegatives([[3,2],[1,0]])) # 0
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 1351.2 - Count Negative Numbers in a Sorted Matrix - Solution 2 - Binary Search Per Row """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def countNegatives(self, grid: List[List[int]]) -> int:
"""Count Negatives Function"""
count = 0
for row in grid:
lo, hi = 0, len(row)
while lo < hi:
mid = (lo + hi) // 2
if row[mid] < 0:
hi = mid
else:
lo = mid + 1
count += len(row) - lo
return count
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().countNegatives([[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]])) # 8
print(Solution().countNegatives([[3,2],[1,0]])) # 0
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 1351.3 - Count Negative Numbers in a Sorted Matrix - Solution 3 - Staircase """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def countNegatives(self, grid: List[List[int]]) -> int:
"""Count Negatives Function"""
m, n = len(grid), len(grid[0])
count = 0
row, col = m - 1, 0
while row >= 0 and col < n:
if grid[row][col] < 0:
count += n - col
row -= 1
else:
col += 1
return count
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().countNegatives([[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]])) # 8
print(Solution().countNegatives([[3,2],[1,0]])) # 0
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()