Skip to content

Commit 5376283

Browse files
authored
Merge pull request #26 from Optimism-Educators/output-main
Output main
2 parents d3e4b66 + c64f824 commit 5376283

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
---
2+
3+
# **2503. Maximum Number of Points From Grid Queries**
4+
5+
## **Problem Statement**
6+
You are given an `m x n` integer matrix `grid` and an array `queries` of size `k`.
7+
8+
Find an array `answer` of size `k` such that for each integer `queries[i]`, you start in the **top-left cell** of the matrix and repeat the following process:
9+
10+
1. If `queries[i]` is **strictly greater** than the value of the current cell, then:
11+
- You **gain one point** if it is your **first time** visiting this cell.
12+
- You can **move** to any adjacent cell **(up, down, left, or right)**.
13+
2. Otherwise, you **stop the process** and gain no more points.
14+
15+
After processing each query, `answer[i]` is the **maximum number of points** you can collect.
16+
- **You can visit the same cell multiple times** for different queries.
17+
18+
Return the resulting array `answer`.
19+
20+
---
21+
22+
## **Example 1**
23+
24+
### **Input:**
25+
```plaintext
26+
grid = [[1,2,3],
27+
[2,5,7],
28+
[3,5,1]]
29+
queries = [5,6,2]
30+
```
31+
32+
### **Output:**
33+
```plaintext
34+
[5,8,1]
35+
```
36+
37+
### **Explanation:**
38+
For each query:
39+
- **Query = 5**: Visit `5` cells.
40+
- **Query = 6**: Visit `8` cells.
41+
- **Query = 2**: Visit `1` cell.
42+
43+
---
44+
45+
## **Example 2**
46+
47+
### **Input:**
48+
```plaintext
49+
grid = [[5,2,1],
50+
[1,1,2]]
51+
queries = [3]
52+
```
53+
54+
### **Output:**
55+
```plaintext
56+
[0]
57+
```
58+
59+
### **Explanation:**
60+
- The **top-left cell (5) is already ≥ 3**, so we **cannot move** and gain **0 points**.
61+
62+
---
63+
64+
## **Constraints**
65+
- `m == grid.length`
66+
- `n == grid[i].length`
67+
- `2 <= m, n <= 1000`
68+
- `4 <= m * n <= 10⁵`
69+
- `k == queries.length`
70+
- `1 <= k <= 10⁴`
71+
- `1 <= grid[i][j], queries[i] <= 10⁶`
72+
73+
---
74+
75+
## **Discussion**
76+
77+
### **Approach 1: Min-Heap + BFS (Optimized Approach)**
78+
- **Use a Min-Heap** to efficiently process the smallest grid values first.
79+
- **Sort queries** in increasing order to process them efficiently.
80+
- **Use BFS (Breadth-First Search)** to expand cells **only when needed**.
81+
- **Store results** for each query and return `answer`.
82+
83+
### **Time Complexity:**
84+
- **O(m * n log(m * n) + k log k)** → Efficient for large grids.
85+
86+
### **Space Complexity:**
87+
- **O(m * n + k)** → Stores visited cells and results.
88+
89+
---
90+
91+
## **🔥 Optimized Solution Using Python**
92+
93+
```python
94+
from heapq import heappush, heappop
95+
96+
def maxPoints(grid, queries):
97+
m, n = len(grid), len(grid[0])
98+
answer = []
99+
queries_sorted = sorted(enumerate(queries), key=lambda x: x[1])
100+
101+
# Min-Heap for BFS
102+
heap = [(grid[0][0], 0, 0)]
103+
visited = set((0, 0))
104+
count = 0
105+
res_map = {}
106+
107+
for idx, query in queries_sorted:
108+
while heap and heap[0][0] < query:
109+
_, r, c = heappop(heap)
110+
count += 1
111+
for dr, dc in [(-1,0), (1,0), (0,-1), (0,1)]:
112+
nr, nc = r + dr, c + dc
113+
if 0 <= nr < m and 0 <= nc < n and (nr, nc) not in visited:
114+
heappush(heap, (grid[nr][nc], nr, nc))
115+
visited.add((nr, nc))
116+
res_map[idx] = count
117+
118+
return [res_map[i] for i in range(len(queries))]
119+
```
120+
121+
---
122+
123+
## 🎯 **How to Contribute**
124+
1. **Fork** the repository.
125+
2. Implement the solution and **test** it with multiple cases.
126+
3. Submit a **pull request** with a clear explanation.
127+
128+
---
129+
130+
## 📂 **File Structure**
131+
```
132+
/solutions
133+
├── max_points_grid_queries.py
134+
├── README.md
135+
```
136+
137+
---
138+
139+
## 🔥 **Want to Participate?**
140+
- Discuss solutions in **GitHub Issues**.
141+
- Improve existing implementations.
142+
143+
---
144+
145+
## 🔗 **Useful Links**
146+
- [LeetCode Problem](https://leetcode.com/problems/maximum-number-of-points-from-grid-queries/)
147+
- [Python Heapq Documentation](https://docs.python.org/3/library/heapq.html)
148+
- [BFS Algorithm Guide](https://www.geeksforgeeks.org/breadth-first-search-or-bfs-for-a-graph/)
149+
150+
---
151+
152+
💡 **Let's learn, code, and grow together! 🚀**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# 2503. Maximum Number of Points From Grid Queries
2+
# https://leetcode.com/problems/maximum-number-of-points-from-grid-queries/
3+
from heapq import heappush, heappop
4+
5+
def maxPoints(grid, queries):
6+
m, n = len(grid), len(grid[0])
7+
answer = []
8+
queries_sorted = sorted(enumerate(queries), key=lambda x: x[1])
9+
10+
# Min-Heap for BFS
11+
heap = [(grid[0][0], 0, 0)]
12+
visited = set((0, 0))
13+
count = 0
14+
res_map = {}
15+
16+
for idx, query in queries_sorted:
17+
while heap and heap[0][0] < query:
18+
_, r, c = heappop(heap)
19+
count += 1
20+
for dr, dc in [(-1,0), (1,0), (0,-1), (0,1)]:
21+
nr, nc = r + dr, c + dc
22+
if 0 <= nr < m and 0 <= nc < n and (nr, nc) not in visited:
23+
heappush(heap, (grid[nr][nc], nr, nc))
24+
visited.add((nr, nc))
25+
res_map[idx] = count
26+
27+
return [res_map[i] for i in range(len(queries))]
28+

0 commit comments

Comments
 (0)