diff --git a/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/README.md b/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/README.md index 4dcd7f3797ce7..a48cd9db4ab3b 100644 --- a/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/README.md +++ b/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/README.md @@ -71,7 +71,7 @@ tags: 输出:[[2,1],[1,2]] 解释:起点为 (2,3) 。 价格范围为 [2,3] ,我们可以选择的物品坐标为 (0,1),(1,1),(1,2) 和 (2,1) 。 -这些物品的排名为: +这些物品的排名为: - (2,1) 距离为 2 ,价格为 2 - (1,2) 距离为 2 ,价格为 3 - (1,1) 距离为 3 @@ -119,7 +119,13 @@ tags: -### 方法一 +### 方法一: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}$ 的行数和列数。 @@ -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 @@ -156,47 +167,46 @@ class Solution: class Solution { public List> 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 items = new ArrayList<>(); + Deque q = new ArrayDeque<>(); + q.offer(new int[] {row, col}); + List 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 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> 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; } @@ -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> items; - if (low <= grid[row][col] && grid[row][col] <= high) - items.emplace_back(0, grid[row][col], row, col); - queue> q; - q.emplace(row, col, 0); + queue> q; + q.push({row, col}); + vector> pq; + if (low <= grid[row][col] && grid[row][col] <= high) { + pq.push_back({0, grid[row][col], row, col}); + } grid[row][col] = 0; vector 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> 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; } @@ -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] } @@ -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; } ``` diff --git a/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/README_EN.md b/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/README_EN.md index 594d962793180..d52629bfa5a31 100644 --- a/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/README_EN.md +++ b/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/README_EN.md @@ -82,11 +82,11 @@ Thus, the 2 highest ranked items in the price range are (2,1) and (1,2). Input: grid = [[1,1,1],[0,0,1],[2,3,4]], pricing = [2,3], start = [0,0], k = 3 Output: [[2,1],[2,0]] Explanation: You start at (0,0). -With a price range of [2,3], we can take items from (2,0) and (2,1). -The ranks of these items are: +With a price range of [2,3], we can take items from (2,0) and (2,1). +The ranks of these items are: - (2,1) with distance 5 - (2,0) with distance 6 -Thus, the 2 highest ranked items in the price range are (2,1) and (2,0). +Thus, the 2 highest ranked items in the price range are (2,1) and (2,0). Note that k = 3 but there are only 2 reachable items within the price range. @@ -114,7 +114,13 @@ Note that k = 3 but there are only 2 reachable items within the price range. -### Solution 1 +### Solution 1: BFS + Sorting + +We can start from $(\textit{row}, \textit{col})$ and use breadth-first search to find all items with prices in the range $[\textit{low}, \textit{high}]$. Store the distance, price, row coordinate, and column coordinate of these items in the array $\textit{pq}$. + +Finally, sort $\textit{pq}$ by distance, price, row coordinate, and column coordinate, and return the coordinates of the first $k$ items. + +The time complexity is $O(m \times n \times \log (m \times n))$, and the space complexity is $O(m \times n)$. Here, $m$ and $n$ are the number of rows and columns of the 2D array $\textit{grid}$, respectively. @@ -126,23 +132,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 @@ -151,47 +162,46 @@ class Solution: class Solution { public List> 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 items = new ArrayList<>(); + Deque q = new ArrayDeque<>(); + q.offer(new int[] {row, col}); + List 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 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> 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; } @@ -207,30 +217,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> items; - if (low <= grid[row][col] && grid[row][col] <= high) - items.emplace_back(0, grid[row][col], row, col); - queue> q; - q.emplace(row, col, 0); + queue> q; + q.push({row, col}); + vector> pq; + if (low <= grid[row][col] && grid[row][col] <= high) { + pq.push_back({0, grid[row][col], row, col}); + } grid[row][col] = 0; vector 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> 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; } @@ -240,34 +256,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] } @@ -279,11 +296,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; } ``` diff --git a/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/Solution.cpp b/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/Solution.cpp index 7713f5b430823..68282f2a05dd0 100644 --- a/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/Solution.cpp +++ b/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/Solution.cpp @@ -4,31 +4,37 @@ class Solution { int m = grid.size(), n = grid[0].size(); int row = start[0], col = start[1]; int low = pricing[0], high = pricing[1]; - vector> items; - if (low <= grid[row][col] && grid[row][col] <= high) - items.emplace_back(0, grid[row][col], row, col); - queue> q; - q.emplace(row, col, 0); + queue> q; + q.push({row, col}); + vector> pq; + if (low <= grid[row][col] && grid[row][col] <= high) { + pq.push_back({0, grid[row][col], row, col}); + } grid[row][col] = 0; vector 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> 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; } -}; \ No newline at end of file +}; diff --git a/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/Solution.go b/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/Solution.go index e6addd6fb66c7..03b8ec04df8b4 100644 --- a/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/Solution.go +++ b/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/Solution.go @@ -1,31 +1,32 @@ -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] } @@ -37,9 +38,8 @@ 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 -} \ No newline at end of file + return +} diff --git a/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/Solution.java b/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/Solution.java index 04822265c81f1..9e1e1cd556e7d 100644 --- a/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/Solution.java +++ b/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/Solution.java @@ -1,48 +1,47 @@ class Solution { public List> 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 items = new ArrayList<>(); + Deque q = new ArrayDeque<>(); + q.offer(new int[] {row, col}); + List 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 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> 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; } -} \ No newline at end of file +} diff --git a/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/Solution.py b/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/Solution.py index d6f839ef00611..354fa88837db3 100644 --- a/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/Solution.py +++ b/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/Solution.py @@ -3,20 +3,25 @@ def highestRankedKItems( 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]] diff --git a/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/Solution.ts b/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/Solution.ts new file mode 100644 index 0000000000000..36242f09c5459 --- /dev/null +++ b/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/Solution.ts @@ -0,0 +1,45 @@ +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; +}