Skip to content

Commit 735b34f

Browse files
chore: add LeetCode daily solution
1 parent fdacc76 commit 735b34f

5 files changed

Lines changed: 148 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Maximum Path Score in a Grid (Medium)
2+
3+
**Problem ID:** 3742
4+
**Date:** 2026-04-30
5+
**Link:** https://leetcode.com/problems/maximum-path-score-in-a-grid/
6+
7+
## Approach
8+
9+
To solve the "Maximum Path Score in a Grid" problem, we can utilize a dynamic programming approach combined with a breadth-first search (BFS) strategy. The main idea is to explore all possible paths from the top-left corner to the bottom-right corner of the grid while keeping track of the accumulated score and cost.
10+
11+
### Approach:
12+
13+
1. **Data Structures**:
14+
- Use a 2D list `dp` where `dp[i][j]` represents the maximum score achievable when reaching cell `(i, j)` with a cost not exceeding `k`.
15+
- A queue (for BFS) to explore the cells in the grid, initialized with the starting cell `(0, 0)`.
16+
17+
2. **Initialization**:
18+
- Start by setting `dp[0][0]` to 0, as starting at the top-left corner incurs no cost and adds no score.
19+
- For all other cells, initialize `dp[i][j]` to -1 (indicating unreachable).
20+
21+
3. **BFS Exploration**:
22+
- While there are cells in the queue, pop the front cell `(i, j)` and explore its neighbors (right `(i, j+1)` and down `(i+1, j)`).
23+
- For each neighbor, calculate the new score and cost based on the value in the grid:
24+
- If the grid value is `0`, it adds `0` to the score and incurs `0` cost.
25+
- If the grid value is `1`, it adds `1` to the score and incurs `1` cost.
26+
- If the grid value is `2`, it adds `2` to the score and incurs `1` cost.
27+
- Check if the new cost exceeds `k`. If it does, skip that neighbor.
28+
- If the new score is greater than the current maximum score recorded in `dp` for that neighbor, update `dp` and add the neighbor to the queue for further exploration.
29+
30+
4. **Final Check**:
31+
- After exploring all possible paths, check the value in `dp[m-1][n-1]`. If it remains `-1`, return `-1` indicating no valid path exists. Otherwise, return the maximum score found.
32+
33+
### Complexity:
34+
- **Time Complexity**: O(m * n), where m is the number of rows and n is the number of columns in the grid. Each cell is processed at most once.
35+
- **Space Complexity**: O(m * n) for the `dp` array and O(m * n) for the queue in the worst case.
36+
37+
This approach ensures that we efficiently explore all valid paths while adhering to the constraints of the problem, ultimately leading to the maximum score achievable without exceeding the specified cost.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int maxPathScore(int[][] grid, int k) {
5+
int m = grid.length, n = grid[0].length;
6+
int[][][] dp = new int[m][n][k + 1];
7+
for (int[][] arr : dp) {
8+
for (int[] a : arr) {
9+
Arrays.fill(a, -1);
10+
}
11+
}
12+
dp[0][0][0] = 0;
13+
14+
for (int cost = 0; cost <= k; cost++) {
15+
for (int i = 0; i < m; i++) {
16+
for (int j = 0; j < n; j++) {
17+
if (dp[i][j][cost] == -1) continue;
18+
int score = dp[i][j][cost];
19+
if (i + 1 < m) {
20+
int newCost = cost + (grid[i + 1][j] == 1 ? 1 : (grid[i + 1][j] == 2 ? 1 : 0));
21+
if (newCost <= k) {
22+
dp[i + 1][j][newCost] = Math.max(dp[i + 1][j][newCost], score + grid[i + 1][j]);
23+
}
24+
}
25+
if (j + 1 < n) {
26+
int newCost = cost + (grid[i][j + 1] == 1 ? 1 : (grid[i][j + 1] == 2 ? 1 : 0));
27+
if (newCost <= k) {
28+
dp[i][j + 1][newCost] = Math.max(dp[i][j + 1][newCost], score + grid[i][j + 1]);
29+
}
30+
}
31+
}
32+
}
33+
}
34+
35+
int maxScore = -1;
36+
for (int cost = 0; cost <= k; cost++) {
37+
maxScore = Math.max(maxScore, dp[m - 1][n - 1][cost]);
38+
}
39+
return maxScore;
40+
}
41+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
maxScore(grid, k) {
3+
const m = grid.length;
4+
const n = grid[0].length;
5+
const dp = Array.from({ length: m }, () => Array.from({ length: n }, () => Array(k + 1).fill(-1)));
6+
dp[0][0][0] = 0;
7+
8+
for (let i = 0; i < m; i++) {
9+
for (let j = 0; j < n; j++) {
10+
for (let cost = 0; cost <= k; cost++) {
11+
if (dp[i][j][cost] === -1) continue;
12+
const score = dp[i][j][cost];
13+
14+
// Move right
15+
if (j + 1 < n) {
16+
const nextCost = cost + (grid[i][j + 1] === 1 ? 1 : grid[i][j + 1] === 2 ? 1 : 0);
17+
const nextScore = score + (grid[i][j + 1] === 1 ? 1 : grid[i][j + 1] === 2 ? 2 : 0);
18+
if (nextCost <= k) {
19+
dp[i][j + 1][nextCost] = Math.max(dp[i][j + 1][nextCost], nextScore);
20+
}
21+
}
22+
23+
// Move down
24+
if (i + 1 < m) {
25+
const nextCost = cost + (grid[i + 1][j] === 1 ? 1 : grid[i + 1][j] === 2 ? 1 : 0);
26+
const nextScore = score + (grid[i + 1][j] === 1 ? 1 : grid[i + 1][j] === 2 ? 2 : 0);
27+
if (nextCost <= k) {
28+
dp[i + 1][j][nextCost] = Math.max(dp[i + 1][j][nextCost], nextScore);
29+
}
30+
}
31+
}
32+
}
33+
}
34+
35+
let maxScore = -1;
36+
for (let cost = 0; cost <= k; cost++) {
37+
maxScore = Math.max(maxScore, dp[m - 1][n - 1][cost]);
38+
}
39+
40+
return maxScore;
41+
}
42+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from collections import deque
2+
3+
class Solution:
4+
def maxPathScore(self, grid: List[List[int]], k: int) -> int:
5+
m, n = len(grid), len(grid[0])
6+
directions = [(1, 0), (0, 1)]
7+
dp = [[-1] * (k + 1) for _ in range(m)]
8+
dp[0][0] = 0
9+
10+
for cost in range(k + 1):
11+
for i in range(m):
12+
for j in range(n):
13+
if dp[i][cost] != -1:
14+
for di, dj in directions:
15+
ni, nj = i + di, j + dj
16+
if ni < m and nj < n:
17+
new_cost = cost
18+
if grid[ni][nj] == 1:
19+
new_cost += 1
20+
elif grid[ni][nj] == 2:
21+
new_cost += 1
22+
if new_cost <= k:
23+
new_score = dp[i][cost] + grid[ni][nj]
24+
dp[ni][new_cost] = max(dp[ni][new_cost], new_score)
25+
26+
max_score = max(dp[m - 1])
27+
return max_score if max_score != -1 else -1

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,3 +439,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
439439
- 2026-04-27 — [Check if There is a Valid Path in a Grid](https://leetcode.com/problems/check-if-there-is-a-valid-path-in-a-grid/) (Medium) → `Medium/2026-04-27-1391-Check-if-There-is-a-Valid-Path-in-a-Grid`
440440
- 2026-04-28 — [Minimum Operations to Make a Uni-Value Grid](https://leetcode.com/problems/minimum-operations-to-make-a-uni-value-grid/) (Medium) → `Medium/2026-04-28-2033-Minimum-Operations-to-Make-a-Uni-Value-Grid`
441441
- 2026-04-29 — [Maximum Score From Grid Operations](https://leetcode.com/problems/maximum-score-from-grid-operations/) (Hard) → `Hard/2026-04-29-3225-Maximum-Score-From-Grid-Operations`
442+
- 2026-04-30 — [Maximum Path Score in a Grid](https://leetcode.com/problems/maximum-path-score-in-a-grid/) (Medium) → `Medium/2026-04-30-3742-Maximum-Path-Score-in-a-Grid`

0 commit comments

Comments
 (0)