Round 1
Questions: Below is my solution attempt to today's daily question, can anyone please help what I am doing wrong, other suggestions are welcome as well. Thanks!!
class Solution { private: bool is_possible(int x, int y, int m , int n){ if(x < 0 || y < 0 || x >= m || y >= n) return false; return true; } void curr(int x, int y, int m, int n, int curr_cost, vector<vector<int>>& min_cost, vector<vector<int>>& grid){ if(x == m-1 && y == n-1){ min_cost[x][y] = min(curr_cost, min_cost[x][y]); return; } if(curr_cost >= min_cost[x][y]) return; min_cost[x][y] = curr_cost; int original_direction = grid[x][y]; if(is_possible(x+1, y, m ,n)) curr(x+1, y, m, n, curr_cost + (1 != original_direction), min_cost, grid); if(is_possible(x-1, y, m ,n)) curr(x-1, y, m, n, curr_cost + (2 != original_direction), min_cost, grid); if(is_possible(x, y+1, m ,n)) curr(x, y+1, m, n, curr_cost + (3 != original_direction), min_cost, grid); if(is_possible(x, y-1, m ,n)) curr(x, y-1, m, n, curr_cost + (4 != original_direction), min_cost, grid); } public: int minCost(vector<vector<int>>& grid) { int m = grid.size(); int n = grid[0].size(); vector<vector<int>> min_cost(m, vector<int>(n, INT_MAX)); curr(0, 0, m, n, 0, min_cost, grid); return min_cost[m-1][n-1]; } };
Candidate's Approach
The candidate implemented a recursive function to explore all possible paths in the grid while keeping track of the current cost. The function checks if the next move is valid and updates the minimum cost accordingly. The candidate is seeking feedback on potential issues with their implementation.
Interviewer's Feedback
No feedback provided.