Skip to content

Commit 6790ea8

Browse files
committed
Add solution and test-cases for problem 1333
1 parent f03226b commit 6790ea8

File tree

3 files changed

+72
-26
lines changed

3 files changed

+72
-26
lines changed

leetcode/1301-1400/1333.Filter-Restaurants-by-Vegan-Friendly-Price-and-Distance/README.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# [1333.Filter Restaurants by Vegan-Friendly, Price and Distance][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given the array `restaurants` where `restaurants[i] = [idi, ratingi, veganFriendlyi, pricei, distancei]`. You have to filter the restaurants using three filters.
5+
6+
The `veganFriendly` filter will be either true (meaning you should only include restaurants with `veganFriendlyi` set to true) or false (meaning you can include any restaurant). In addition, you have the filters `maxPrice` and `maxDistance` which are the maximum value for price and distance of restaurants you should consider respectively.
7+
8+
Return the array of restaurant **IDs** after filtering, ordered by **rating** from highest to lowest. For restaurants with the same rating, order them by **id** from highest to lowest. For simplicity `veganFriendlyi` and `veganFriendly` take value 1 when it is true, and 0 when it is false.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
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
14+
Output: [3,1,5]
15+
Explanation:
16+
The restaurants are:
17+
Restaurant 1 [id=1, rating=4, veganFriendly=1, price=40, distance=10]
18+
Restaurant 2 [id=2, rating=8, veganFriendly=0, price=50, distance=5]
19+
Restaurant 3 [id=3, rating=8, veganFriendly=1, price=30, distance=4]
20+
Restaurant 4 [id=4, rating=10, veganFriendly=0, price=10, distance=3]
21+
Restaurant 5 [id=5, rating=1, veganFriendly=1, price=15, distance=1]
22+
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).
1323
```
1424

15-
## 题意
16-
> ...
17-
18-
## 题解
25+
**Example 2:**
1926

20-
### 思路1
21-
> ...
22-
Filter Restaurants by Vegan-Friendly, Price and Distance
23-
```go
2427
```
28+
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
29+
Output: [4,3,2,1,5]
30+
Explanation: The restaurants are the same as in example 1, but in this case the filter veganFriendly = 0, therefore all restaurants are considered.
31+
```
32+
33+
**Example 3:**
2534

35+
```
36+
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
37+
Output: [4,5]
38+
```
2639

2740
## 结语
2841

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(restaurants [][]int, veganFriendly int, maxPrice int, maxDistance int) []int {
6+
left := make([]int, 0)
7+
for i := 0; i < len(restaurants); i++ {
8+
if veganFriendly == 1 && restaurants[i][2] == 0 {
9+
continue
10+
}
11+
if restaurants[i][3] > maxPrice {
12+
continue
13+
}
14+
if restaurants[i][4] > maxDistance {
15+
continue
16+
}
17+
left = append(left, i)
18+
}
19+
sort.Slice(left, func(i, j int) bool {
20+
a, b := restaurants[left[i]], restaurants[left[j]]
21+
if a[1] == b[1] {
22+
return a[0] > b[0]
23+
}
24+
return a[1] > b[1]
25+
})
26+
ret := make([]int, len(left))
27+
for i, idx := range left {
28+
ret[i] = restaurants[idx][0]
29+
}
30+
return ret
531
}

leetcode/1301-1400/1333.Filter-Restaurants-by-Vegan-Friendly-Price-and-Distance/Solution_test.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,38 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
restaurants [][]int
14+
veganFriendly, maxPrice, maxDistance int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", [][]int{
18+
{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},
19+
}, 1, 50, 10, []int{3, 1, 5}},
20+
{"TestCase2", [][]int{
21+
{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},
22+
}, 0, 50, 10, []int{4, 3, 2, 1, 5}},
23+
{"TestCase3", [][]int{
24+
{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},
25+
}, 0, 30, 3, []int{4, 5}},
1926
}
2027

2128
// 开始测试
2229
for i, c := range cases {
2330
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
31+
got := Solution(c.restaurants, c.veganFriendly, c.maxPrice, c.maxDistance)
2532
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
33+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v %v",
34+
c.expect, got, c.restaurants, c.veganFriendly, c.maxPrice, c.maxDistance)
2835
}
2936
})
3037
}
3138
}
3239

33-
// 压力测试
40+
// 压力测试
3441
func BenchmarkSolution(b *testing.B) {
3542
}
3643

37-
// 使用案列
44+
// 使用案列
3845
func ExampleSolution() {
3946
}

0 commit comments

Comments
 (0)