Rating changes for last rounds are temporarily rolled back. They will be returned soon. ×

What is the time complexity for this modified BFS code?

Revision en2, by _Satoru_, 2022-10-07 07:21:45

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?

#### History

Revisions

Rev. Lang. By When Δ Comment
en3 _Satoru_ 2022-10-07 11:46:44 38
en2 _Satoru_ 2022-10-07 07:21:45 2311
en1 _Satoru_ 2022-10-06 23:22:10 466 Initial revision (published)