From 6790ea881318f5787a778a1bc011101ca2599427 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Tue, 30 Sep 2025 14:27:21 +0800 Subject: [PATCH] Add solution and test-cases for problem 1333 --- .../README.md | 39 ++++++++++++------- .../Solution.go | 30 +++++++++++++- .../Solution_test.go | 29 ++++++++------ 3 files changed, 72 insertions(+), 26 deletions(-) diff --git a/leetcode/1301-1400/1333.Filter-Restaurants-by-Vegan-Friendly-Price-and-Distance/README.md b/leetcode/1301-1400/1333.Filter-Restaurants-by-Vegan-Friendly-Price-and-Distance/README.md index 7cd7a8ffb..4742f174d 100644 --- a/leetcode/1301-1400/1333.Filter-Restaurants-by-Vegan-Friendly-Price-and-Distance/README.md +++ b/leetcode/1301-1400/1333.Filter-Restaurants-by-Vegan-Friendly-Price-and-Distance/README.md @@ -1,28 +1,41 @@ # [1333.Filter Restaurants by Vegan-Friendly, Price and Distance][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +Given the array `restaurants` where `restaurants[i] = [idi, ratingi, veganFriendlyi, pricei, distancei]`. You have to filter the restaurants using three filters. + +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. + +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. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +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 +Output: [3,1,5] +Explanation: +The restaurants are: +Restaurant 1 [id=1, rating=4, veganFriendly=1, price=40, distance=10] +Restaurant 2 [id=2, rating=8, veganFriendly=0, price=50, distance=5] +Restaurant 3 [id=3, rating=8, veganFriendly=1, price=30, distance=4] +Restaurant 4 [id=4, rating=10, veganFriendly=0, price=10, distance=3] +Restaurant 5 [id=5, rating=1, veganFriendly=1, price=15, distance=1] +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). ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Filter Restaurants by Vegan-Friendly, Price and Distance -```go ``` +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 +Output: [4,3,2,1,5] +Explanation: The restaurants are the same as in example 1, but in this case the filter veganFriendly = 0, therefore all restaurants are considered. +``` + +**Example 3:** +``` +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 +Output: [4,5] +``` ## 结语 diff --git a/leetcode/1301-1400/1333.Filter-Restaurants-by-Vegan-Friendly-Price-and-Distance/Solution.go b/leetcode/1301-1400/1333.Filter-Restaurants-by-Vegan-Friendly-Price-and-Distance/Solution.go index d115ccf5e..ec406ab1b 100644 --- a/leetcode/1301-1400/1333.Filter-Restaurants-by-Vegan-Friendly-Price-and-Distance/Solution.go +++ b/leetcode/1301-1400/1333.Filter-Restaurants-by-Vegan-Friendly-Price-and-Distance/Solution.go @@ -1,5 +1,31 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func Solution(restaurants [][]int, veganFriendly int, maxPrice int, maxDistance int) []int { + left := make([]int, 0) + for i := 0; i < len(restaurants); i++ { + if veganFriendly == 1 && restaurants[i][2] == 0 { + continue + } + if restaurants[i][3] > maxPrice { + continue + } + if restaurants[i][4] > maxDistance { + continue + } + left = append(left, i) + } + sort.Slice(left, func(i, j int) bool { + a, b := restaurants[left[i]], restaurants[left[j]] + if a[1] == b[1] { + return a[0] > b[0] + } + return a[1] > b[1] + }) + ret := make([]int, len(left)) + for i, idx := range left { + ret[i] = restaurants[idx][0] + } + return ret } diff --git a/leetcode/1301-1400/1333.Filter-Restaurants-by-Vegan-Friendly-Price-and-Distance/Solution_test.go b/leetcode/1301-1400/1333.Filter-Restaurants-by-Vegan-Friendly-Price-and-Distance/Solution_test.go index 14ff50eb4..4ce6a110f 100644 --- a/leetcode/1301-1400/1333.Filter-Restaurants-by-Vegan-Friendly-Price-and-Distance/Solution_test.go +++ b/leetcode/1301-1400/1333.Filter-Restaurants-by-Vegan-Friendly-Price-and-Distance/Solution_test.go @@ -9,31 +9,38 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + restaurants [][]int + veganFriendly, maxPrice, maxDistance int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", [][]int{ + {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}, + }, 1, 50, 10, []int{3, 1, 5}}, + {"TestCase2", [][]int{ + {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}, + }, 0, 50, 10, []int{4, 3, 2, 1, 5}}, + {"TestCase3", [][]int{ + {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}, + }, 0, 30, 3, []int{4, 5}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.restaurants, c.veganFriendly, c.maxPrice, c.maxDistance) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v %v", + c.expect, got, c.restaurants, c.veganFriendly, c.maxPrice, c.maxDistance) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }