06. Letters Collection
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
To solve this problem, I just found the left, right, up, and down boundaries for the given cell and then added the elements present in those boundaries.
To do so I follow these steps
Initialize an empty vector called out
to store the results.
For each query, iterate through the cells in the matrix within a 'hop' distance from the given coordinates (x, y
).
For each cell (i, j)
within the hop
distance, check if it is within the boundaries of the matrix (i.e., i >= 0, i < n, j >= 0, and j < m)
.
If the conditions are met, add the values of the matrix cells at (i, y - hop)
and (i, y + hop)
to the sum
if j
is within bounds. Similarly, add the values of the matrix cells at (x - hop, j)
and (x + hop, j)
to the sum
if i is within bounds.
Append the sum
to the out
vector for the current query. Return the out
vector containing the results for all queries.
Time Complexity: O(q * (hop^2))
where q
is the number of queries and 'hop' is the hop distance.
Auxiliary Space Complexity: The auxiliary space is used to store the results in the out vector, which takes O(q)
space.
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the repository.