leetcode 1368 (0-1 dp)

这篇博客介绍了如何利用宽度优先搜索(BFS)策略解决LeetCode上的1368题。在网格中,从左上角到右下角的路径成本由相邻单元格之间的差值决定,0成本的单元格优先级更高。通过维护一个双端队列,博主提出了一种解决方案,先将0成本单元格加入队列前端,1成本单元格加入队列尾部,并在遍历过程中更新单元格状态。当找到目标位置时返回累计成本,若未找到则返回0。
摘要由CSDN通过智能技术生成

Thoughts on leetcode 1368

https://leetcode.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid/

We consider the cell that can be reached with no cost at the same level as the previous cell. Therefore, we maintain a two-end queue so that 0-cost cells are inserted into the front and 1-cost cells are inserted into the end. Also, we keep track of the current level size.

Pay attention that different from the previous BFS, we need to set the cell as visited after it is popped from the queue. This is because the cost of the cell can be re-evaluated (The problem is more like a simplified version of dijkstra)

template

for all v in vertices:
	dist[v] = inf
dist[source] = 0;
deque d
d.push_front(source)
while d.empty() == false:
	vertex = get front element and pop as in BFS.
	for all edges e of form (vertex , u):
		if travelling e relaxes distance to u:
			relax dist[u]
			if e.weight = 1:
				d.push_back(u)
			else:
				d.push_front(u)

Solution for leetcode1368

class Solution {
    public int minCost(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        Deque<int[]>queue=new ArrayDeque();
        int[][]directions={{0,0},{0,1},{0,-1},{1,0},{-1,0}};
        boolean[][] visited = new boolean[m][n];
        queue.add(new int[]{0,0});
        int ans = 0;
        while(queue.size() != 0){
            int size = queue.size();
            while(size != 0){
                int[] cur = queue.pollFirst();
                size--;
                int curx = cur[0];
                int cury = cur[1];
                visited[curx][cury] = true;
                if(curx == m - 1 && cury == n - 1){
                    return ans;
                }
                for(int i = 1; i < directions.length; i++){
                    int newx = cur[0] + directions[i][0];
                    int newy = cur[1] + directions[i][1];
                    if(newx < 0 || newx >= m || newy < 0 || newy >= n || visited[newx][newy] == true ){
                        continue;
                    }
                    //there is no cost to go to this cell from the last cell
                    // we can consider them on the same level
                    if(i == grid[curx][cury]){
                        queue.addFirst(new int[]{newx, newy});
                        size++;
                    }
                    else{
                        queue.addLast(new int[]{newx,newy});
                    }       
                }
            }
            
            ans++;
        }
        return 0;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值