Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
1,151 views
in Online Assessments by Expert (44,360 points) | 1,151 views

2 Answers

0 like 0 dislike
Best answer

First question is exactly same as https://leetcode.com/problems/maximal-network-rank/
Second one is similar to https://leetcode.com/problems/unique-paths-iii/

 

Got 70 min to solve these

by Expert (44,360 points)
0 like 0 dislike

Q2 Solution

 

class Solution {
    public int uniquePathsIII(int[][] maze) {
        int sr=0,sc=0,obs=0;
        
        for (int i = 0; i < maze.length; i++) {
            for (int j = i; j < maze[i].length; j++) {
                if (maze[i][j]==1) {
                    sr = i;
                    sc = j;
                }
                if(maze[i][j]==-1){
                    obs +=1;
                }
            }
        }
        
        return pathAll2("",maze,sr,sc,obs);
    }
    
    public static int pathAll2(String p, int[][] maze, int r, int c,int obs){
        //if(r==maze.length-1&& c==maze[0].length-1){
        if(maze[r][c]==2){
            if(p.length()==(maze.length*maze[0].length)-1-obs){
            //System.out.println(p);
            //System.out.println();
            return 1;
            }
            return 0;
        }
        if(maze[r][c]==-1){
            return 0;
        }
        int count =0;
        maze[r][c]=-1;       
        if(r<maze.length-1){
            count += pathAll2(p+'D', maze, r+1, c,obs);
        }
        if(c<maze[0].length-1){
            count += pathAll2(p+'R', maze, r, c+1,obs);
        }
        if(r>0){
            count += pathAll2(p+'U', maze, r-1, c,obs);
        }
        if(c>0){
            count += pathAll2(p+'L', maze, r, c-1,obs);
        }
        maze[r][c]=0;
        return count;
    }
}
by Expert (44,360 points)