|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <openset.wang@gmail.com> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../remove-palindromic-subsequences "Remove Palindromic Subsequences") |
| 9 | + |
| 10 | +[Next >](../find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance "Find the City With the Smallest Number of Neighbors at a Threshold Distance") |
| 11 | + |
| 12 | +## [1333. Filter Restaurants by Vegan-Friendly, Price and Distance (Medium)](https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance "餐厅过滤器") |
| 13 | + |
| 14 | +<p>Given the array <code>restaurants</code> where <code>restaurants[i] = [id<sub>i</sub>, rating<sub>i</sub>, veganFriendly<sub>i</sub>, price<sub>i</sub>, distance<sub>i</sub>]</code>. You have to filter the restaurants using three filters.</p> |
| 15 | + |
| 16 | +<p>The <code>veganFriendly</code> filter will be either <em>true</em> (meaning you should only include restaurants with <code>veganFriendly<sub>i</sub></code> set to true) or <em>false</em> (meaning you can include any restaurant). In addition, you have the filters <code>maxPrice</code> and <code>maxDistance</code> which are the maximum value for price and distance of restaurants you should consider respectively.</p> |
| 17 | + |
| 18 | +<p>Return the array of restaurant <em><strong>IDs</strong></em> after filtering, ordered by <strong>rating</strong> from highest to lowest. For restaurants with the same rating, order them by <em><strong>id</strong></em> from highest to lowest. For simplicity <code>veganFriendly<sub>i</sub></code> and <code>veganFriendly</code> take value <em>1</em> when it is <em>true</em>, and <em>0</em> when it is <em>false</em>.</p> |
| 19 | + |
| 20 | +<p> </p> |
| 21 | +<p><strong>Example 1:</strong></p> |
| 22 | + |
| 23 | +<pre> |
| 24 | +<strong>Input:</strong> restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], veganFriendly = 1, maxPrice = 50, maxDistance = 10 |
| 25 | +<strong>Output:</strong> [3,1,5] |
| 26 | +<strong>Explanation: |
| 27 | +</strong>The restaurants are: |
| 28 | +Restaurant 1 [id=1, rating=4, veganFriendly=1, price=40, distance=10] |
| 29 | +Restaurant 2 [id=2, rating=8, veganFriendly=0, price=50, distance=5] |
| 30 | +Restaurant 3 [id=3, rating=8, veganFriendly=1, price=30, distance=4] |
| 31 | +Restaurant 4 [id=4, rating=10, veganFriendly=0, price=10, distance=3] |
| 32 | +Restaurant 5 [id=5, rating=1, veganFriendly=1, price=15, distance=1] |
| 33 | +After filter restaurants with veganFriendly = 1, maxPrice = 50 and maxDistance = 10 we have restaurant 3, restaurant 1 and restaurant 5 (ordered by rating from highest to lowest). |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p><strong>Example 2:</strong></p> |
| 37 | + |
| 38 | +<pre> |
| 39 | +<strong>Input:</strong> restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], veganFriendly = 0, maxPrice = 50, maxDistance = 10 |
| 40 | +<strong>Output:</strong> [4,3,2,1,5] |
| 41 | +<strong>Explanation:</strong> The restaurants are the same as in example 1, but in this case the filter veganFriendly = 0, therefore all restaurants are considered. |
| 42 | +</pre> |
| 43 | + |
| 44 | +<p><strong>Example 3:</strong></p> |
| 45 | + |
| 46 | +<pre> |
| 47 | +<strong>Input:</strong> restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], veganFriendly = 0, maxPrice = 30, maxDistance = 3 |
| 48 | +<strong>Output:</strong> [4,5] |
| 49 | +</pre> |
| 50 | + |
| 51 | +<p> </p> |
| 52 | +<p><strong>Constraints:</strong></p> |
| 53 | + |
| 54 | +<ul> |
| 55 | + <li><code>1 <= restaurants.length <= 10^4</code></li> |
| 56 | + <li><code>restaurants[i].length == 5</code></li> |
| 57 | + <li><code>1 <= id<sub>i</sub>, rating<sub>i</sub>, price<sub>i</sub>, distance<sub>i </sub><= 10^5</code></li> |
| 58 | + <li><code>1 <= maxPrice, maxDistance <= 10^5</code></li> |
| 59 | + <li><code>veganFriendly<sub>i</sub></code> and <code>veganFriendly</code> are 0 or 1.</li> |
| 60 | + <li>All <code>id<sub>i</sub></code> are distinct.</li> |
| 61 | +</ul> |
| 62 | + |
| 63 | +### Related Topics |
| 64 | + [[Sort](../../tag/sort/README.md)] |
| 65 | + [[Array](../../tag/array/README.md)] |
| 66 | + |
| 67 | +### Hints |
| 68 | +<details> |
| 69 | +<summary>Hint 1</summary> |
| 70 | +Do the filtering and sort as said. Note that the id may not be the index in the array. |
| 71 | +</details> |
0 commit comments