Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save SuryaPratapK/77cc428520cc32ae7a53540b063cbe5a to your computer and use it in GitHub Desktop.

Select an option

Save SuryaPratapK/77cc428520cc32ae7a53540b063cbe5a to your computer and use it in GitHub Desktop.
class Solution {
public:
vector<string> createGrid(int m, int n, int k) {
vector<string> grid;
//Step-1: Fill grid with obstacles except 1st row and last col. This is for 1st path
// On a 1*N or M*1 grid, only 1 path is possible
for(int i=0;i<m;++i){
if(i==0)
grid.push_back(string(n,'.'));//all free cells
else
grid.push_back(string(n-1,'#') + '.');//n-1 obstacles and last cell free
}
//Step-2: Check if 2nd path can be formed
if(k>=2){
if(m>=2 and n>=2)
grid[1][n-2] = '.';
else
return {};
}
//Step-3: Check if 3rd path can be formed
// Either extend the 1st row or 1st col
// Remember: Here m>=2 and n>=2
if(k>=3){
if(m>=3 and m>=n)
grid[2][n-2] = '.';
else if(n>=3 and n>m)
grid[1][n-3] = '.';
else
return {};
}
//Step-4: Check if 4th path can be formed
// 3 ways: 1st row, 1st col, special arrangement for 3*3 grid
// Remember: Here m>=2 and n>=2 and one of the m,n is atleast >= 3
if(k==4){
if(m>=4 and m>=n)
grid[3][n-2] = '.';
else if(n>=4 and n>m)
grid[1][n-4] = '.';
else if(m==3 and n==3)
return {"..#","...","#.."};
else
return {};
}
return grid;
}
};
/*
//JAVA
class Solution {
public List<String> createGrid(int m, int n, int k) {
List<char[]> temp = new ArrayList<>();
// Step-1: Fill grid with obstacles except 1st row and last col. This is for 1st path
// On a 1*N or M*1 grid, only 1 path is possible
for (int i = 0; i < m; i++) {
char[] row = new char[n];
if (i == 0) {
Arrays.fill(row, '.');
} else {
Arrays.fill(row, '#');
row[n - 1] = '.';
}
temp.add(row);
}
// Step-2: Check if 2nd path can be formed
if (k >= 2) {
if (m >= 2 && n >= 2)
temp.get(1)[n - 2] = '.';
else
return new ArrayList<>();
}
// Step-3: Check if 3rd path can be formed
// Either extend the 1st row or 1st col
// Remember: Here m>=2 and n>=2
if (k >= 3) {
if (m >= 3 && m >= n)
temp.get(2)[n - 2] = '.';
else if (n >= 3 && n > m)
temp.get(1)[n - 3] = '.';
else
return new ArrayList<>();
}
// Step-4: Check if 4th path can be formed
// 3 ways: 1st row, 1st col, special arrangement for 3*3 grid
// Remember: Here m>=2 and n>=2 and one of the m,n is atleast >= 3
if (k == 4) {
if (m >= 4 && m >= n)
temp.get(3)[n - 2] = '.';
else if (n >= 4 && n > m)
temp.get(1)[n - 4] = '.';
else if (m == 3 && n == 3)
return Arrays.asList("..#", "...", "#..");
else
return new ArrayList<>();
}
List<String> grid = new ArrayList<>();
for (char[] row : temp)
grid.add(new String(row));
return grid;
}
}
#Python
class Solution:
def createGrid(self, m: int, n: int, k: int) -> List[str]:
grid = []
# Step-1: Fill grid with obstacles except 1st row and last col. This is for 1st path
# On a 1*N or M*1 grid, only 1 path is possible
for i in range(m):
if i == 0:
grid.append(['.'] * n)
else:
grid.append(['#'] * (n - 1) + ['.'])
# Step-2: Check if 2nd path can be formed
if k >= 2:
if m >= 2 and n >= 2:
grid[1][n - 2] = '.'
else:
return []
# Step-3: Check if 3rd path can be formed
# Either extend the 1st row or 1st col
# Remember: Here m>=2 and n>=2
if k >= 3:
if m >= 3 and m >= n:
grid[2][n - 2] = '.'
elif n >= 3 and n > m:
grid[1][n - 3] = '.'
else:
return []
# Step-4: Check if 4th path can be formed
# 3 ways: 1st row, 1st col, special arrangement for 3*3 grid
# Remember: Here m>=2 and n>=2 and one of the m,n is atleast >= 3
if k == 4:
if m >= 4 and m >= n:
grid[3][n - 2] = '.'
elif n >= 4 and n > m:
grid[1][n - 4] = '.'
elif m == 3 and n == 3:
return ["..#", "...", "#.."]
else:
return []
return [''.join(row) for row in grid]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment