### _Satoru_'s blog

By _Satoru_, history, 2 months ago,

You are given a 0-indexed 2D integer array grid of size m x n. Each cell has one of two values: 0 represents an empty cell, 1 represents an obstacle that may be removed. Return the minimum number of obstacles to remove so you can move from the upper left corner (0, 0) to the lower right corner (m — 1, n — 1). Constraints: 1 <= m, n <= 1e5 , 2 <= m * n <= 1e5

My Code

The solution is just a BFS where you push a neighbor of the current cell being processed if it can be reached after removal of lesser number of obstacles. Due to this a cell maybe pushed multiple times to the queue. I am not able to convince myself that the time complexity is O(nm) which it should be as per the constraints of the problem. How can we prove that each cell will be updated constant times?

• +4

 » 2 months ago, # |   +6 Auto comment: topic has been updated by _Satoru_ (previous revision, new revision, compare).
 » 2 months ago, # |   0 Auto comment: topic has been updated by _Satoru_ (previous revision, new revision, compare).
 » 2 months ago, # |   0 You can try the worst case.The time complexity of your code is at least $O(nm)$
•  » » 2 months ago, # ^ |   0 What will be the upper bound? It should be O(nm(n+m)) according to me but in that case the above code shouldn't be accepted but it does get accepted.
 » 2 months ago, # |   +11 The test cases are weak. If you try it on a test with n=2, m=4k+1 that looks like01000001repeated k times with a column of 2 0s at the end, your code would be very slow.