Skip to content

Commit 5f8c724

Browse files
committed
1333_Filter_Restaurants_by_Vegan-Friendly_Price_and_Distance
1 parent daeec70 commit 5f8c724

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
func filterRestaurants(_ restaurants: [[Int]], _ veganFriendly: Int, _ maxPrice: Int, _ maxDistance: Int) -> [Int] {
3+
var filteredRestaurants = [[Int]]()
4+
for restaurant in restaurants {
5+
let (id, rating, veganFriendliness, price, distance) = (restaurant[0], restaurant[1], restaurant[2], restaurant[3], restaurant[4])
6+
if veganFriendly == 1 && veganFriendliness == 0 {
7+
continue
8+
}
9+
if price > maxPrice || distance > maxDistance {
10+
continue
11+
}
12+
filteredRestaurants.append(restaurant)
13+
}
14+
filteredRestaurants.sort {
15+
if $0[1] != $1[1] {
16+
return $0[1] > $1[1]
17+
} else {
18+
return $0[0] > $1[0]
19+
}
20+
}
21+
22+
var result = [Int]()
23+
filteredRestaurants.forEach {
24+
result.append($0[0])
25+
}
26+
return result
27+
}
28+
}
29+
30+
/*
31+
veganFriendly == 1 > take only 1
32+
veganFriendly == 0 > take all
33+
34+
1. By rating, decending
35+
2. By ID, decending
36+
3. price <= maxPrice
37+
4. distance <= maxDistance
38+
39+
result [IDs]
40+
*/

0 commit comments

Comments
 (0)