Class Solution
java.lang.Object
g2101_2200.s2146_k_highest_ranked_items_within_a_price_range.Solution
2146 - K Highest Ranked Items Within a Price Range.<p>Medium</p>
<p>You are given a <strong>0-indexed</strong> 2D integer array <code>grid</code> of size <code>m x n</code> that represents a map of the items in a shop. The integers in the grid represent the following:</p>
<ul>
<li><code>0</code> represents a wall that you cannot pass through.</li>
<li><code>1</code> represents an empty cell that you can freely move to and from.</li>
<li>All other positive integers represent the price of an item in that cell. You may also freely move to and from these item cells.</li>
</ul>
<p>It takes <code>1</code> step to travel between adjacent grid cells.</p>
<p>You are also given integer arrays <code>pricing</code> and <code>start</code> where <code>pricing = [low, high]</code> and <code>start = [row, col]</code> indicates that you start at the position <code>(row, col)</code> and are interested only in items with a price in the range of <code>[low, high]</code> ( <strong>inclusive</strong> ). You are further given an integer <code>k</code>.</p>
<p>You are interested in the <strong>positions</strong> of the <code>k</code> <strong>highest-ranked</strong> items whose prices are <strong>within</strong> the given price range. The rank is determined by the <strong>first</strong> of these criteria that is different:</p>
<ol>
<li>Distance, defined as the length of the shortest path from the <code>start</code> ( <strong>shorter</strong> distance has a higher rank).</li>
<li>Price ( <strong>lower</strong> price has a higher rank, but it must be <strong>in the price range</strong> ).</li>
<li>The row number ( <strong>smaller</strong> row number has a higher rank).</li>
<li>The column number ( <strong>smaller</strong> column number has a higher rank).</li>
</ol>
<p>Return <em>the</em> <code>k</code> <em>highest-ranked items within the price range <strong>sorted</strong> by their rank (highest to lowest)</em>. If there are fewer than <code>k</code> reachable items within the price range, return <em><strong>all</strong> of them</em>.</p>
<p><strong>Example 1:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2021/12/16/example1drawio.png" alt="" /></p>
<p><strong>Input:</strong> grid = [[1,2,0,1],[1,3,0,1],[0,2,5,1]], pricing = [2,5], start = [0,0], k = 3</p>
<p><strong>Output:</strong> [[0,1],[1,1],[2,1]]</p>
<p><strong>Explanation:</strong> You start at (0,0).</p>
<p>With a price range of [2,5], we can take items from (0,1), (1,1), (2,1) and (2,2).</p>
<p>The ranks of these items are:</p>
<ul>
<li>
<p>(0,1) with distance 1</p>
</li>
<li>
<p>(1,1) with distance 2</p>
</li>
<li>
<p>(2,1) with distance 3</p>
</li>
<li>
<p>(2,2) with distance 4</p>
</li>
</ul>
<p>Thus, the 3 highest ranked items in the price range are (0,1), (1,1), and (2,1).</p>
<p><strong>Example 2:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2021/12/16/example2drawio1.png" alt="" /></p>
<p><strong>Input:</strong> grid = [[1,2,0,1],[1,3,3,1],[0,2,5,1]], pricing = [2,3], start = [2,3], k = 2</p>
<p><strong>Output:</strong> [[2,1],[1,2]]</p>
<p><strong>Explanation:</strong> You start at (2,3).</p>
<p>With a price range of [2,3], we can take items from (0,1), (1,1), (1,2) and (2,1).</p>
<p>The ranks of these items are:</p>
<ul>
<li>
<p>(2,1) with distance 2, price 2</p>
</li>
<li>
<p>(1,2) with distance 2, price 3</p>
</li>
<li>
<p>(1,1) with distance 3</p>
</li>
<li>
<p>(0,1) with distance 4</p>
</li>
</ul>
<p>Thus, the 2 highest ranked items in the price range are (2,1) and (1,2).</p>
<p><strong>Example 3:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2021/12/30/example3.png" alt="" /></p>
<p><strong>Input:</strong> grid = [[1,1,1],[0,0,1],[2,3,4]], pricing = [2,3], start = [0,0], k = 3</p>
<p><strong>Output:</strong> [[2,1],[2,0]]</p>
<p><strong>Explanation:</strong> You start at (0,0).</p>
<p>With a price range of [2,3], we can take items from (2,0) and (2,1).</p>
<p>The ranks of these items are:</p>
<ul>
<li>
<p>(2,1) with distance 5</p>
</li>
<li>
<p>(2,0) with distance 6</p>
</li>
</ul>
<p>Thus, the 2 highest ranked items in the price range are (2,1) and (2,0).</p>
<pre><code> Note that k = 3 but there are only 2 reachable items within the price range.
</code></pre>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= grid[i][j] <= 10<sup>5</sup></code></li>
<li><code>pricing.length == 2</code></li>
<li><code>2 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>start.length == 2</code></li>
<li><code>0 <= row <= m - 1</code></li>
<li><code>0 <= col <= n - 1</code></li>
<li><code>grid[row][col] > 0</code></li>
<li><code>1 <= k <= m * n</code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionhighestRankedKItems(int[][] grid, int[] pricing, int[] start, int k)
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
highestRankedKItems
-