|
| 1 | +/** |
| 2 | + * [2146] K Highest Ranked Items Within a Price Range |
| 3 | + * |
| 4 | + * You are given a 0-indexed 2D integer array grid of size m x n that represents a map of the items in a shop. The integers in the grid represent the following: |
| 5 | + * |
| 6 | + * 0 represents a wall that you cannot pass through. |
| 7 | + * 1 represents an empty cell that you can freely move to and from. |
| 8 | + * All other positive integers represent the price of an item in that cell. You may also freely move to and from these item cells. |
| 9 | + * |
| 10 | + * It takes 1 step to travel between adjacent grid cells. |
| 11 | + * You are also given integer arrays pricing and start where pricing = [low, high] and start = [row, col] indicates that you start at the position (row, col) and are interested only in items with a price in the range of [low, high] (inclusive). You are further given an integer k. |
| 12 | + * You are interested in the positions of the k highest-ranked items whose prices are within the given price range. The rank is determined by the first of these criteria that is different: |
| 13 | + * <ol> |
| 14 | + * Distance, defined as the length of the shortest path from the start (shorter distance has a higher rank). |
| 15 | + * Price (lower price has a higher rank, but it must be in the price range). |
| 16 | + * The row number (smaller row number has a higher rank). |
| 17 | + * The column number (smaller column number has a higher rank). |
| 18 | + * </ol> |
| 19 | + * Return the k highest-ranked items within the price range sorted by their rank (highest to lowest). If there are fewer than k reachable items within the price range, return all of them. |
| 20 | + * |
| 21 | + * Example 1: |
| 22 | + * <img alt="" src="https://assets.leetcode.com/uploads/2021/12/16/example1drawio.png" style="width: 200px; height: 151px;" /> |
| 23 | + * Input: grid = [[1,2,0,1],[1,3,0,1],[0,2,5,1]], pricing = [2,5], start = [0,0], k = 3 |
| 24 | + * Output: [[0,1],[1,1],[2,1]] |
| 25 | + * Explanation: You start at (0,0). |
| 26 | + * With a price range of [2,5], we can take items from (0,1), (1,1), (2,1) and (2,2). |
| 27 | + * The ranks of these items are: |
| 28 | + * - (0,1) with distance 1 |
| 29 | + * - (1,1) with distance 2 |
| 30 | + * - (2,1) with distance 3 |
| 31 | + * - (2,2) with distance 4 |
| 32 | + * Thus, the 3 highest ranked items in the price range are (0,1), (1,1), and (2,1). |
| 33 | + * |
| 34 | + * Example 2: |
| 35 | + * <img alt="" src="https://assets.leetcode.com/uploads/2021/12/16/example2drawio1.png" style="width: 200px; height: 151px;" /> |
| 36 | + * Input: grid = [[1,2,0,1],[1,3,3,1],[0,2,5,1]], pricing = [2,3], start = [2,3], k = 2 |
| 37 | + * Output: [[2,1],[1,2]] |
| 38 | + * Explanation: You start at (2,3). |
| 39 | + * With a price range of [2,3], we can take items from (0,1), (1,1), (1,2) and (2,1). |
| 40 | + * The ranks of these items are: |
| 41 | + * - (2,1) with distance 2, price 2 |
| 42 | + * - (1,2) with distance 2, price 3 |
| 43 | + * - (1,1) with distance 3 |
| 44 | + * - (0,1) with distance 4 |
| 45 | + * Thus, the 2 highest ranked items in the price range are (2,1) and (1,2). |
| 46 | + * |
| 47 | + * Example 3: |
| 48 | + * <img alt="" src="https://assets.leetcode.com/uploads/2021/12/30/example3.png" style="width: 149px; height: 150px;" /> |
| 49 | + * Input: grid = [[1,1,1],[0,0,1],[2,3,4]], pricing = [2,3], start = [0,0], k = 3 |
| 50 | + * Output: [[2,1],[2,0]] |
| 51 | + * Explanation: You start at (0,0). |
| 52 | + * With a price range of [2,3], we can take items from (2,0) and (2,1). |
| 53 | + * The ranks of these items are: |
| 54 | + * - (2,1) with distance 5 |
| 55 | + * - (2,0) with distance 6 |
| 56 | + * Thus, the 2 highest ranked items in the price range are (2,1) and (2,0). |
| 57 | + * Note that k = 3 but there are only 2 reachable items within the price range. |
| 58 | + * |
| 59 | + * |
| 60 | + * Constraints: |
| 61 | + * |
| 62 | + * m == grid.length |
| 63 | + * n == grid[i].length |
| 64 | + * 1 <= m, n <= 10^5 |
| 65 | + * 1 <= m * n <= 10^5 |
| 66 | + * 0 <= grid[i][j] <= 10^5 |
| 67 | + * pricing.length == 2 |
| 68 | + * 2 <= low <= high <= 10^5 |
| 69 | + * start.length == 2 |
| 70 | + * 0 <= row <= m - 1 |
| 71 | + * 0 <= col <= n - 1 |
| 72 | + * grid[row][col] > 0 |
| 73 | + * 1 <= k <= m * n |
| 74 | + * |
| 75 | + */ |
| 76 | +pub struct Solution {} |
| 77 | + |
| 78 | +// problem: https://leetcode.com/problems/k-highest-ranked-items-within-a-price-range/ |
| 79 | +// discuss: https://leetcode.com/problems/k-highest-ranked-items-within-a-price-range/discuss/?currentPage=1&orderBy=most_votes&query= |
| 80 | + |
| 81 | +// submission codes start here |
| 82 | + |
| 83 | +impl Solution { |
| 84 | + pub fn highest_ranked_k_items( |
| 85 | + grid: Vec<Vec<i32>>, |
| 86 | + pricing: Vec<i32>, |
| 87 | + start: Vec<i32>, |
| 88 | + k: i32, |
| 89 | + ) -> Vec<Vec<i32>> { |
| 90 | + vec![] |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// submission codes end |
| 95 | + |
| 96 | +#[cfg(test)] |
| 97 | +mod tests { |
| 98 | + use super::*; |
| 99 | + |
| 100 | + #[test] |
| 101 | + #[ignore] |
| 102 | + fn test_2146_example_1() { |
| 103 | + let grid = vec![vec![1, 2, 0, 1], vec![1, 3, 0, 1], vec![0, 2, 5, 1]]; |
| 104 | + let pricing = vec![2, 5]; |
| 105 | + let start = vec![0, 0]; |
| 106 | + let k = 3; |
| 107 | + |
| 108 | + let result = vec![vec![0, 1], vec![1, 1], vec![2, 1]]; |
| 109 | + |
| 110 | + assert_eq!( |
| 111 | + Solution::highest_ranked_k_items(grid, pricing, start, k), |
| 112 | + result |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + #[test] |
| 117 | + #[ignore] |
| 118 | + fn test_2146_example_2() { |
| 119 | + let grid = vec![vec![1, 2, 0, 1], vec![1, 3, 3, 1], vec![0, 2, 5, 1]]; |
| 120 | + let pricing = vec![2, 3]; |
| 121 | + let start = vec![2, 3]; |
| 122 | + let k = 2; |
| 123 | + |
| 124 | + let result = vec![vec![2, 1], vec![1, 2]]; |
| 125 | + |
| 126 | + assert_eq!( |
| 127 | + Solution::highest_ranked_k_items(grid, pricing, start, k), |
| 128 | + result |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + #[test] |
| 133 | + #[ignore] |
| 134 | + fn test_2146_example_3() { |
| 135 | + let grid = vec![vec![1, 1, 1], vec![0, 0, 1], vec![2, 3, 4]]; |
| 136 | + let pricing = vec![2, 3]; |
| 137 | + let start = vec![0, 0]; |
| 138 | + let k = 3; |
| 139 | + |
| 140 | + let result = vec![vec![2, 1], vec![2, 0]]; |
| 141 | + |
| 142 | + assert_eq!( |
| 143 | + Solution::highest_ranked_k_items(grid, pricing, start, k), |
| 144 | + result |
| 145 | + ); |
| 146 | + } |
| 147 | +} |
0 commit comments