LeetCode Legends!
L2 : 2D Arrays
2D Arrays
What is a 2D Array?
This is when we have arrays as the elements of another array:
Example:
arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
Here we access row i and column j with arr[i][j]
Legends.add(u)
© Phillip Tellier 2023
Example Problem:
Number of Negative Numbers (Sorted)
Given a 2D Array of integers that is sorted in non-increasing order (from largest to smallest) in both the columns and the rows, write a function the counts the number of negative entries.
Example instance:
Answer here would be 5
Legends.add(u)
© Phillip Tellier 2023
Naive Approach: Brute Force
Iterate through every entry increasing your count when you come across a negative integer:
Runtime: Θ(r × c)
Memory: Θ(1)
Legends.add(u)
© Phillip Tellier 2023
A Faster Solution
Iterate through every entry increasing your count when you come across a negative integer:
Runtime: Θ(# of negative entries)
Memory: Θ(1)
Legends.add(u)
© Phillip Tellier 2023