_Satoru_'s blog

By _Satoru_, history, 18 months ago, In English

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

Problem Link: https://leetcode.com/problems/minimum-obstacle-removal-to-reach-corner/

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?

Full text and comments »

  • Vote: I like it
  • +4
  • Vote: I do not like it