Skip to content

Commit 6161beb

Browse files
add 1333
1 parent 11bdae6 commit 6161beb

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1333|[Filter Restaurants by Vegan-Friendly, Price and Distance](https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1333.java) | |Medium||
1112
|1331|[Rank Transform of an Array](https://leetcode.com/problems/rank-transform-of-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1331.java) | |Easy||
1213
|1317|[Convert Integer to the Sum of Two No-Zero Integers](https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1317.java) | |Easy||
1314
|1305|[All Elements in Two Binary Search Trees](https://leetcode.com/problems/all-elements-in-two-binary-search-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1305.java) | |Medium||
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
/**
9+
* 1333. Filter Restaurants by Vegan-Friendly, Price and Distance
10+
*
11+
* Given the array restaurants where restaurants[i] = [idi, ratingi, veganFriendlyi, pricei, distancei].
12+
* You have to filter the restaurants using three filters.
13+
* The veganFriendly filter will be either true (meaning you should only include restaurants with
14+
* veganFriendlyi set to true) or false (meaning you can include any restaurant).
15+
* In addition, you have the filters maxPrice and maxDistance which are the maximum value
16+
* for price and distance of restaurants you should consider respectively.
17+
* Return the array of restaurant IDs after filtering, ordered by rating from highest to lowest.
18+
* For restaurants with the same rating, order them by id from highest to lowest.
19+
* For simplicity veganFriendlyi and veganFriendly take value 1 when it is true, and 0 when it is false.
20+
*
21+
* Example 1:
22+
* Input: 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
23+
* Output: [3,1,5]
24+
* Explanation:
25+
* The restaurants are:
26+
* Restaurant 1 [id=1, rating=4, veganFriendly=1, price=40, distance=10]
27+
* Restaurant 2 [id=2, rating=8, veganFriendly=0, price=50, distance=5]
28+
* Restaurant 3 [id=3, rating=8, veganFriendly=1, price=30, distance=4]
29+
* Restaurant 4 [id=4, rating=10, veganFriendly=0, price=10, distance=3]
30+
* Restaurant 5 [id=5, rating=1, veganFriendly=1, price=15, distance=1]
31+
* 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).
32+
*
33+
* Example 2:
34+
* Input: 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
35+
* Output: [4,3,2,1,5]
36+
* Explanation: The restaurants are the same as in example 1, but in this case the filter veganFriendly = 0, therefore all restaurants are considered.
37+
*
38+
* Example 3:
39+
* Input: 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
40+
* Output: [4,5]
41+
*
42+
* Constraints:
43+
* 1 <= restaurants.length <= 10^4
44+
* restaurants[i].length == 5
45+
* 1 <= idi, ratingi, pricei, distancei <= 10^5
46+
* 1 <= maxPrice, maxDistance <= 10^5
47+
* veganFriendlyi and veganFriendly are 0 or 1.
48+
* All idi are distinct.
49+
* */
50+
public class _1333 {
51+
public static class Solution1 {
52+
public List<Integer> filterRestaurants(int[][] restaurants, int veganFriendly, int maxPrice, int maxDistance) {
53+
List<int[]> list = new ArrayList<>();
54+
for (int[] restaurant : restaurants) {
55+
if (((veganFriendly == 1 && restaurant[2] == 1) || veganFriendly == 0)
56+
&& restaurant[3] <= maxPrice && restaurant[4] <= maxDistance) {
57+
list.add(restaurant);
58+
}
59+
}
60+
Collections.sort(list, (a, b) -> b[1] - a[1] == 0 ? b[0] - a[0] : b[1] - a[1]);
61+
return list.stream().map(restaurant -> restaurant[0]).collect(Collectors.toList());
62+
}
63+
}
64+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1333;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class _1333Test {
12+
private static _1333.Solution1 solution1;
13+
private static int[][] restaurants;
14+
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _1333.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
restaurants = new int[][]{
23+
{1, 4, 1, 40, 10},
24+
{2, 8, 0, 50, 5},
25+
{3, 8, 1, 30, 4},
26+
{4, 10, 0, 10, 3},
27+
{5, 1, 1, 15, 1}
28+
};
29+
assertEquals(Arrays.asList(3, 1, 5), solution1.filterRestaurants(restaurants, 1, 50, 10));
30+
}
31+
32+
@Test
33+
public void test2() {
34+
restaurants = new int[][]{
35+
{1, 4, 1, 40, 10},
36+
{2, 8, 0, 50, 5},
37+
{3, 8, 1, 30, 4},
38+
{4, 10, 0, 10, 3},
39+
{5, 1, 1, 15, 1}
40+
};
41+
assertEquals(Arrays.asList(4, 3, 2, 1, 5), solution1.filterRestaurants(restaurants, 0, 50, 10));
42+
}
43+
44+
@Test
45+
public void test3() {
46+
restaurants = new int[][]{
47+
{1, 4, 1, 40, 10},
48+
{2, 8, 0, 50, 5},
49+
{3, 8, 1, 30, 4},
50+
{4, 10, 0, 10, 3},
51+
{5, 1, 1, 15, 1}
52+
};
53+
assertEquals(Arrays.asList(4, 5), solution1.filterRestaurants(restaurants, 0, 30, 3));
54+
}
55+
56+
}

0 commit comments

Comments
 (0)