2328. Number of Increasing Paths in a Grid | Recursion | LeetCode Daily Challenge

preview_player
Показать описание

0:00​ - Question Understanding
2:40 - Observations
6:00 - Approach
9:25 - Code
16:00 - Complexity

#coding #dsa #leetcode #daily #programming #cpp #tutorial
Рекомендации по теме
Комментарии
Автор

class Solution {
public:
int mod = 1e9+7;
int dp[1001][1001];
int solve(vector<vector<int>>& grid, int i, int j, int prev) {
int m = grid.size(), n = grid[0].size();
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] <= prev) {
return 0;
}
if (dp[i][j] != -1) {
return dp[i][j];
}
int up = solve(grid, i-1, j, grid[i][j]);
int down = solve(grid, i+1, j, grid[i][j]);
int left = solve(grid, i, j-1, grid[i][j]);
int right = solve(grid, i, j+1, grid[i][j]);
return dp[i][j] = (1 + up + down + left + right) % mod;
}

int grid) {
int ans = 0, m = grid.size(), n = grid[0].size();
memset(dp, -1, sizeof(dp));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
ans = (ans + solve(grid, i, j, -1)) % mod;
}
}
return ans;
}
};

deepintotech
Автор

I recently completed arrays, strings, stacks, binary search, and linked list questions on LeetCode. Mostly easy and medium questions. However, I still don't feel very comfortable when it comes to solving random problems from these topics. So, should I work on these topics more or complete the rest of the DSA like greedy, dynamic programming (DP), graphs, tries, etc., and then start doing more problems outside my comfort zone?

novascotia
Автор

I implemented the simmiler logic. But when I tried to run my code without using memoization it gave me wrong answer. I expected TLE. I was just curious so I ran the code without memoization. But When I run the code with memoization it works and submits. The logic is exactly the same. I was using Set to keep track of the visited node each time I ran dfs on each node. Would mind taking a look at my code? It's in JS. Thank you the explanation was very good. I skipped the video and just saw the memoize part I got it in like 30 sec. Thank you brother 😄😄

aadil
join shbcf.ru