Skip to content

Commit 8188875

Browse files
committed
Add solution #5320
1 parent 330fa98 commit 8188875

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 5320. Filter Restaurants by Vegan-Friendly, Price and Distance
3+
* https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/
4+
* Difficulty: Medium
5+
*
6+
* Given the array restaurants where restaurants[i] = [idi, ratingi, veganFriendlyi, pricei, distancei].
7+
* You have to filter the restaurants using three filters.
8+
*
9+
* The veganFriendly filter will be either true (meaning you should only include restaurants with
10+
* veganFriendlyi set to true) or false (meaning you can include any restaurant). In addition, you
11+
* have the filters maxPrice and maxDistance which are the maximum value for price and distance of
12+
* restaurants you should consider respectively.
13+
*
14+
* Return the array of restaurant IDs after filtering, ordered by rating from highest to lowest.
15+
* For restaurants with the same rating, order them by id from highest to lowest. For simplicity
16+
* veganFriendlyi and veganFriendly take value 1 when it is true, and 0 when it is false.
17+
*/
18+
19+
/**
20+
* @param {number[][]} restaurants
21+
* @param {number} veganFriendly
22+
* @param {number} maxPrice
23+
* @param {number} maxDistance
24+
* @return {number[]}
25+
*/
26+
var filterRestaurants = function(restaurants, veganFriendly, maxPrice, maxDistance) {
27+
return restaurants.filter(r => (!veganFriendly || r[2]) && r[3] <= maxPrice && r[4] <= maxDistance)
28+
.sort((a, b) => a[1] === b[1] ? b[0] - a[0] : b[1] - a[1])
29+
.map(r => r[0]);
30+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
1318|[Minimum Flips to Make a OR b Equal to c](./1318-minimum-flips-to-make-a-or-b-equal-to-c.js)|Medium|
6464
1323|[Maximum 69 Number](./1323-maximum-69-number.js)|Easy|
6565
1324|[Print Words Vertically](./1324-print-words-vertically.js)|Medium|
66+
5320|[Filter Restaurants by Vegan-Friendly, Price and Distance](./5320-filter-restaurants-by-vegan-friendly-price-and-distance.js)|Medium|
6667

6768
## License
6869

0 commit comments

Comments
 (0)