Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.2146 (#3510)
Browse files Browse the repository at this point in the history
No.2146.K Highest Ranked Items Within a Price Range
  • Loading branch information
yanglbme authored Sep 11, 2024
1 parent 9f4cdb7 commit 01bf266
Show file tree
Hide file tree
Showing 7 changed files with 448 additions and 261 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ tags:
<b>输出:</b>[[2,1],[1,2]]
<b>解释:</b>起点为 (2,3) 。
价格范围为 [2,3] ,我们可以选择的物品坐标为 (0,1),(1,1),(1,2) 和 (2,1) 。
这些物品的排名为:
这些物品的排名为:
- (2,1) 距离为 2 ,价格为 2
- (1,2) 距离为 2 ,价格为 3
- (1,1) 距离为 3
Expand Down Expand Up @@ -119,7 +119,13 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:BFS + 排序

我们可以从 $(\textit{row}, \textit{col})$ 出发,使用广度优先搜索找到所有价格在 $[\textit{low}, \textit{high}]$ 范围内的物品,将这些物品的距离、价格、行坐标和列坐标存入数组 $\textit{pq}$ 中。

最后对 $\textit{pq}$ 按照距离、价格、行坐标和列坐标的顺序排序,取前 $k$ 个物品的坐标返回。

时间复杂度 $O(m \times n \times \log (m \times n))$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是二维数组 $\textit{grid}$ 的行数和列数。

<!-- tabs:start -->

Expand All @@ -131,23 +137,28 @@ class Solution:
self, grid: List[List[int]], pricing: List[int], start: List[int], k: int
) -> List[List[int]]:
m, n = len(grid), len(grid[0])
row, col, low, high = start + pricing
items = []
row, col = start
low, high = pricing
q = deque([(row, col)])
pq = []
if low <= grid[row][col] <= high:
items.append([0, grid[row][col], row, col])
q = deque([(row, col, 0)])
pq.append((0, grid[row][col], row, col))
grid[row][col] = 0
dirs = (-1, 0, 1, 0, -1)
step = 0
while q:
i, j, d = q.popleft()
for a, b in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
x, y = i + a, j + b
if 0 <= x < m and 0 <= y < n and grid[x][y]:
if low <= grid[x][y] <= high:
items.append([d + 1, grid[x][y], x, y])
q.append((x, y, d + 1))
grid[x][y] = 0
items.sort()
return [item[2:] for item in items][:k]
step += 1
for _ in range(len(q)):
x, y = q.popleft()
for a, b in pairwise(dirs):
nx, ny = x + a, y + b
if 0 <= nx < m and 0 <= ny < n and grid[nx][ny] > 0:
if low <= grid[nx][ny] <= high:
pq.append((step, grid[nx][ny], nx, ny))
grid[nx][ny] = 0
q.append((nx, ny))
pq.sort()
return [list(x[2:]) for x in pq[:k]]
```

#### Java
Expand All @@ -156,47 +167,46 @@ class Solution:
class Solution {
public List<List<Integer>> highestRankedKItems(
int[][] grid, int[] pricing, int[] start, int k) {
int m = grid.length, n = grid[0].length;
int m = grid.length;
int n = grid[0].length;
int row = start[0], col = start[1];
int low = pricing[0], high = pricing[1];
List<int[]> items = new ArrayList<>();
Deque<int[]> q = new ArrayDeque<>();
q.offer(new int[] {row, col});
List<int[]> pq = new ArrayList<>();
if (low <= grid[row][col] && grid[row][col] <= high) {
items.add(new int[] {0, grid[row][col], row, col});
pq.add(new int[] {0, grid[row][col], row, col});
}
grid[row][col] = 0;
Deque<int[]> q = new ArrayDeque<>();
q.offer(new int[] {row, col, 0});
int[] dirs = {-1, 0, 1, 0, -1};
while (!q.isEmpty()) {
int[] p = q.poll();
int i = p[0], j = p[1], d = p[2];
for (int l = 0; l < 4; ++l) {
int x = i + dirs[l], y = j + dirs[l + 1];
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] > 0) {
if (low <= grid[x][y] && grid[x][y] <= high) {
items.add(new int[] {d + 1, grid[x][y], x, y});
final int[] dirs = {-1, 0, 1, 0, -1};
for (int step = 1; !q.isEmpty(); ++step) {
for (int size = q.size(); size > 0; --size) {
int[] curr = q.poll();
int x = curr[0], y = curr[1];
for (int j = 0; j < 4; j++) {
int nx = x + dirs[j];
int ny = y + dirs[j + 1];
if (0 <= nx && nx < m && 0 <= ny && ny < n && grid[nx][ny] > 0) {
if (low <= grid[nx][ny] && grid[nx][ny] <= high) {
pq.add(new int[] {step, grid[nx][ny], nx, ny});
}
grid[nx][ny] = 0;
q.offer(new int[] {nx, ny});
}
grid[x][y] = 0;
q.offer(new int[] {x, y, d + 1});
}
}
}
items.sort((a, b) -> {
if (a[0] != b[0]) {
return a[0] - b[0];
}
if (a[1] != b[1]) {
return a[1] - b[1];
}
if (a[2] != b[2]) {
return a[2] - b[2];
}
return a[3] - b[3];

pq.sort((a, b) -> {
if (a[0] != b[0]) return Integer.compare(a[0], b[0]);
if (a[1] != b[1]) return Integer.compare(a[1], b[1]);
if (a[2] != b[2]) return Integer.compare(a[2], b[2]);
return Integer.compare(a[3], b[3]);
});

List<List<Integer>> ans = new ArrayList<>();
for (int i = 0; i < items.size() && i < k; ++i) {
int[] p = items.get(i);
ans.add(Arrays.asList(p[2], p[3]));
for (int i = 0; i < Math.min(k, pq.size()); i++) {
ans.add(List.of(pq.get(i)[2], pq.get(i)[3]));
}
return ans;
}
Expand All @@ -212,30 +222,36 @@ public:
int m = grid.size(), n = grid[0].size();
int row = start[0], col = start[1];
int low = pricing[0], high = pricing[1];
vector<tuple<int, int, int, int>> items;
if (low <= grid[row][col] && grid[row][col] <= high)
items.emplace_back(0, grid[row][col], row, col);
queue<tuple<int, int, int>> q;
q.emplace(row, col, 0);
queue<pair<int, int>> q;
q.push({row, col});
vector<tuple<int, int, int, int>> pq;
if (low <= grid[row][col] && grid[row][col] <= high) {
pq.push_back({0, grid[row][col], row, col});
}
grid[row][col] = 0;
vector<int> dirs = {-1, 0, 1, 0, -1};
while (!q.empty()) {
auto [i, j, d] = q.front();
q.pop();
for (int l = 0; l < 4; ++l) {
int x = i + dirs[l], y = j + dirs[l + 1];
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y]) {
if (low <= grid[x][y] && grid[x][y] <= high) items.emplace_back(d + 1, grid[x][y], x, y);
grid[x][y] = 0;
q.emplace(x, y, d + 1);
for (int step = 1; q.size(); ++step) {
int sz = q.size();
for (int i = 0; i < sz; ++i) {
auto [x, y] = q.front();
q.pop();
for (int j = 0; j < 4; ++j) {
int nx = x + dirs[j];
int ny = y + dirs[j + 1];
if (0 <= nx && nx < m && 0 <= ny && ny < n && grid[nx][ny] > 0) {
if (low <= grid[nx][ny] && grid[nx][ny] <= high) {
pq.push_back({step, grid[nx][ny], nx, ny});
}
grid[nx][ny] = 0;
q.push({nx, ny});
}
}
}
}
sort(items.begin(), items.end());
sort(pq.begin(), pq.end());
vector<vector<int>> ans;
for (int i = 0; i < items.size() && i < k; ++i) {
auto [d, p, x, y] = items[i];
ans.push_back({x, y});
for (int i = 0; i < min(k, (int) pq.size()); ++i) {
ans.push_back({get<2>(pq[i]), get<3>(pq[i])});
}
return ans;
}
Expand All @@ -245,34 +261,35 @@ public:
#### Go
```go
func highestRankedKItems(grid [][]int, pricing []int, start []int, k int) [][]int {
func highestRankedKItems(grid [][]int, pricing []int, start []int, k int) (ans [][]int) {
m, n := len(grid), len(grid[0])
row, col := start[0], start[1]
low, high := pricing[0], pricing[1]
var items [][]int
q := [][2]int{{row, col}}
pq := [][]int{}
if low <= grid[row][col] && grid[row][col] <= high {
items = append(items, []int{0, grid[row][col], row, col})
pq = append(pq, []int{0, grid[row][col], row, col})
}
q := [][]int{{row, col, 0}}
grid[row][col] = 0
dirs := []int{-1, 0, 1, 0, -1}
for len(q) > 0 {
p := q[0]
q = q[1:]
i, j, d := p[0], p[1], p[2]
for l := 0; l < 4; l++ {
x, y := i+dirs[l], j+dirs[l+1]
if x >= 0 && x < m && y >= 0 && y < n && grid[x][y] > 0 {
if low <= grid[x][y] && grid[x][y] <= high {
items = append(items, []int{d + 1, grid[x][y], x, y})
dirs := [5]int{-1, 0, 1, 0, -1}
for step := 1; len(q) > 0; step++ {
for sz := len(q); sz > 0; sz-- {
x, y := q[0][0], q[0][1]
q = q[1:]
for j := 0; j < 4; j++ {
nx, ny := x+dirs[j], y+dirs[j+1]
if nx >= 0 && nx < m && ny >= 0 && ny < n && grid[nx][ny] > 0 {
if low <= grid[nx][ny] && grid[nx][ny] <= high {
pq = append(pq, []int{step, grid[nx][ny], nx, ny})
}
grid[nx][ny] = 0
q = append(q, [2]int{nx, ny})
}
grid[x][y] = 0
q = append(q, []int{x, y, d + 1})
}
}
}
sort.Slice(items, func(i, j int) bool {
a, b := items[i], items[j]
sort.Slice(pq, func(i, j int) bool {
a, b := pq[i], pq[j]
if a[0] != b[0] {
return a[0] < b[0]
}
Expand All @@ -284,11 +301,60 @@ func highestRankedKItems(grid [][]int, pricing []int, start []int, k int) [][]in
}
return a[3] < b[3]
})
var ans [][]int
for i := 0; i < len(items) && i < k; i++ {
ans = append(ans, items[i][2:])
for i := 0; i < len(pq) && i < k; i++ {
ans = append(ans, pq[i][2:])
}
return ans
return
}
```

#### TypeScript

```ts
function highestRankedKItems(
grid: number[][],
pricing: number[],
start: number[],
k: number,
): number[][] {
const [m, n] = [grid.length, grid[0].length];
const [row, col] = start;
const [low, high] = pricing;
let q: [number, number][] = [[row, col]];
const pq: [number, number, number, number][] = [];
if (low <= grid[row][col] && grid[row][col] <= high) {
pq.push([0, grid[row][col], row, col]);
}
grid[row][col] = 0;
const dirs = [-1, 0, 1, 0, -1];
for (let step = 1; q.length > 0; ++step) {
const nq: [number, number][] = [];
for (const [x, y] of q) {
for (let j = 0; j < 4; j++) {
const nx = x + dirs[j];
const ny = y + dirs[j + 1];
if (nx >= 0 && nx < m && ny >= 0 && ny < n && grid[nx][ny] > 0) {
if (low <= grid[nx][ny] && grid[nx][ny] <= high) {
pq.push([step, grid[nx][ny], nx, ny]);
}
grid[nx][ny] = 0;
nq.push([nx, ny]);
}
}
}
q = nq;
}
pq.sort((a, b) => {
if (a[0] !== b[0]) return a[0] - b[0];
if (a[1] !== b[1]) return a[1] - b[1];
if (a[2] !== b[2]) return a[2] - b[2];
return a[3] - b[3];
});
const ans: number[][] = [];
for (let i = 0; i < Math.min(k, pq.length); i++) {
ans.push([pq[i][2], pq[i][3]]);
}
return ans;
}
```

Expand Down
Loading

0 comments on commit 01bf266

Please sign in to comment.