Zheaoli / do-something-right

MIT License
37 stars 3 forks source link

2022-01-29 #130

Open Zheaoli opened 2 years ago

Zheaoli commented 2 years ago

2022-01-29

lvcq commented 2 years ago
/*
 * @lc app=leetcode.cn id=1765 lang=typescript
 *
 * [1765] 地图中的最高点
 */

// @lc code=start
function highestPeak(isWater: number[][]): number[][] {
    const row = isWater.length;
    const col = isWater[0].length;
    let list:Array<Array<number>> =[];
    for(let i=0;i<row;i++){
        for(let j=0;j<col;j++){
            if(isWater[i][j]===0){
                isWater[i][j]=-1;
            }else{
                isWater[i][j]=0;
                list.push([i,j]);
            }
        }
    }
    while(list.length>0){
        const nlist:Array<Array<number>>=[];
        list.forEach(([x,y])=>{
            const next = isWater[x][y]+1;
            ([[x,y-1],[x+1,y],[x,y+1],[x-1,y]]).filter(([dx,dy])=>{
                return dx>=0&&dx<row&&dy>=0&&dy<col&&isWater[dx][dy]===-1
            }).forEach(([dx,dy])=>{
                isWater[dx][dy]=next;
                nlist.push([dx,dy]);
            });
        })
        list = nlist;
    }   
    return isWater;
};
// @lc code=end

微信id: 风华引 来自 vscode 插件