Leet Code 第 134 场周赛

1033. 移动石子直到连续

题目链接

需要特判一下间隔为1和间隔为2的情况

class Solution {
    public int[] numMovesStones(int a, int b, int c) {
        int x1 = a, x2 = b, x3 = c;
        a = Math.min(Math.min(x1,x2),x3);
        c = Math.max(Math.max(x1,x2),x3);
        b = x1+x2+x3-a-c;
        int[] res = new int[2];
        res[0] = Math.min(1,c-b-1) + Math.min(1,b-a-1);
        if(b-a ==2 || c -b == 2) res[0] = Math.min(res[0],1);
        res[1] = c-a-2;
        return res;
    }
}

1034. 边框着色

题目链接

注意题意,是联通分量的边界染色。直接dfs+判断边界即可。

class Solution {
    
    int fx[] = {0,0,1,-1};
    int fy[] = {1,-1,0,0};
    int[][] vis;
    int n,m;
    
    boolean check(int x, int y)
    {
        if(x < 0 || y < 0 || x >= n || y >= m) return false;
        return true;
    }
    
    
    void dfs(int [][] grid, int x, int y, int p)
    {

        for(int i = 0; i < 4; i++)
        {
            int ix = x + fx[i];
            int iy = y + fy[i];
            if(check(ix,iy) && grid[ix][iy] == p)
            {
                if(vis[ix][iy] == 0)
                {
                vis[ix][iy] = 1;
                dfs(grid,ix,iy,p);
                }
            }
            else {
                vis[x][y] = -1;
            }
        }
    }
    
    public int[][] colorBorder(int[][] grid, int r0, int c0, int color) {
        n = grid.length;
        m = grid[0].length;
        vis = new int[n][m];
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                vis[i][j] = 0;
        int p = grid[r0][c0];
        vis[r0][c0] = 1;
        dfs(grid,r0,c0,p);
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                if(vis[i][j] == -1) grid[i][j] = color;
        return grid;
}
}

1035. 不相交的线

题目链接

直接DP, dp[i][j]表示第一行的前i个和第二行的前j个进行匹配的最优结果。dp方程见代码!

class Solution {
    public int maxUncrossedLines(int[] A, int[] B) {
        int[][] dp = new int[A.length][B.length];
        for(int i = 0; i < A.length; i++)
            for(int j = 0; j < B.length; j++)
               dp[i][j] = 0;
                
        for(int i = 0; i < A.length; i++)
        {
            if(i != 0) dp[i][0] = dp[i-1][0];
            if(A[i] == B[0]) dp[i][0] = Math.max(dp[i][0],1);
        }
        for(int i = 0; i < B.length; i++)
        {
            if(i != 0) dp[0][i] = dp[0][i-1];
            if(A[0] == B[i]) dp[0][i] = Math.max(dp[0][i],1);
        }
        for(int i = 1; i < A.length; i++)
            for(int j = 1; j < B.length; j++)
            {
                dp[i][j] = Math.max(dp[i][j],dp[i-1][j-1]);
                dp[i][j] = Math.max(dp[i][j],dp[i-1][j]);
                dp[i][j] = Math.max(dp[i][j],dp[i][j-1]);
                if(A[i] == B[j]) 
                 dp[i][j] = Math.max(dp[i][j],dp[i-1][j-1]+1);
                //System.out.println(i+" "+ j + " " + dp[i][j]);
            }
        return dp[A.length-1][B.length-1];
    }
}

1036. 逃离大迷宫

题目链接

这道题起初一看觉得是到简单的搜索题,但是看到数据量就知道简单的暴力搜索肯定会TLE,所以我们需要想想办法,然后注意观察题意,其中标注了障碍物最多200个,所以障碍物围成的圈的直径一定小于200,所以我们只需要考虑两种情况,一种是障碍物将两个点围在了一起,那么我们只需要简单的dfs搜索就好了,限制一下搜索的直径。第二种是障碍物只围了一个点,所以我们需要分别从起点和终点出发,做两个搜索,判断其是否被围住,即是否能到达直径为200的圈外,如果其都能到达圈外,则说明起点可以到达终点。

class Solution {
    
    public int[] fx = {0,0,1,-1};
    public int[] fy = {1,-1,0,0};
    public int sx,sy;
    public boolean[][] vis;
    public boolean[][] g;
    boolean check(int x, int y)
    {
        if(x < 0 || y < 0 || x >= 1e6 || y >= 1e6) return false;
        return true;
    }
    boolean check1(int x,int y)
    {
        int ix = x-sx+200;
        int iy = y-sy+200;
        if(ix < 0 || iy < 0 || ix >= 400 || iy >= 400) return false;
        return true;
    }
    
    
    boolean dfs1(int x, int y, int tx, int ty)
    {
         if(Math.abs(x-sx) + Math.abs(y-sx) >= 200) return false;
        if(x == tx && y == ty) return true;
        for(int i = 0; i < 4; i++)
        {
            int ix = x + fx[i];
            int iy = y + fy[i];
            if(check(ix,iy) && check1(ix,iy) && !vis[ix-sx+200][iy-sy+200] && !g[ix-sx+200][iy-sy+200])
            {
                vis[ix-sx+200][iy-sy+200] = true;
                if(dfs1(ix,iy,tx,ty))
                return true;
            }
        }
        return false;
    }
    
    boolean dfs2(int x, int y)
    {
        if(Math.abs(x-sx) + Math.abs(y-sx) >= 200) return true;
         for(int i = 0; i < 4; i++)
        {
            int ix = x + fx[i];
            int iy = y + fy[i];
            if(check(ix,iy) && check1(ix,iy) && !vis[ix-sx+200][iy-sy+200] && !g[ix-sx+200][iy-sy+200])
            {
                vis[ix-sx+200][iy-sy+200] = true;
                if(dfs2(ix,iy))
                return true;
            }
        }
        return false;
    }
    
        
        
    public boolean isEscapePossible(int[][] blocked, int[] source, int[] target) {
        
        boolean res;
        vis = new boolean[400][400];
        g = new boolean[400][400];
        for(int i = 0; i < 400; i++)
            for(int j = 0; j < 400; j++)
                g[i][j] = vis[i][j] = false;
        sx = source[0];sy = source[1];
        for(int i = 0; i < blocked.length; i++)
        {
            int ix = blocked[i][0] - source[0] + 200;
            int iy = blocked[i][1] - source[1] + 200;
            if(check1(blocked[i][0],blocked[i][1])) g[ix][iy] = true;
        }
        vis[200][200] = true; 
        res =  dfs1(source[0],source[1],target[0],target[1]);
        if(res) return res;
        
        res = true;
        
        for(int i = 0; i < 400; i++)
            for(int j = 0; j < 400; j++)
                vis[i][j] = false;
        vis[200][200] = true;
        res = res && dfs2(source[0],source[1]);
        
        sx = target[0]; sy = target[1];
        for(int i = 0; i < 400; i++)
            for(int j = 0; j < 400; j++)
                g[i][j] = vis[i][j] = false;
        for(int i = 0; i < blocked.length; i++)
        {
            int ix = blocked[i][0] - target[0] + 200;
            int iy = blocked[i][1] - target[1] + 200;
             if(check1(blocked[i][0],blocked[i][1])) g[ix][iy] = true;
        }
        res = res && dfs2(target[0],target[1]);    
        return res;
    }
}

 

你可能感兴趣的:(leetcode,比赛套题)