Advertisement

Filter Restaurants by Vegan-Friendly, Price and Distance - LeetCode 1333 Solution

Filter Restaurants by Vegan-Friendly, Price and Distance - Complete Solution Guide

Filter Restaurants by Vegan-Friendly, Price and Distance is LeetCode problem 1333, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given the array restaurants where restaurants[i] = [id i , rating i , veganFriendly i , price i , distance i ] . You have to filter the restaurants using three filters. The veganFriendly filter will be either true (meaning you should only include restaurants with veganFriendly i 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 respectivel

Detailed Explanation

The problem asks us to filter a list of restaurants based on three criteria: vegan-friendliness, maximum price, and maximum distance. Each restaurant is represented as a list of five integers: `[id, rating, veganFriendly, price, distance]`. We are given a `veganFriendly` filter (0 or 1), a `maxPrice`, and a `maxDistance`. The goal is to return a list of restaurant IDs that satisfy these filters, sorted first by rating in descending order, and then by ID in descending order for restaurants with the same rating.

Solution Approach

The solution involves two main steps: filtering and sorting. First, we iterate through the restaurants list and keep only the restaurants that satisfy the vegan-friendly, max price, and max distance criteria. Then, we sort the filtered restaurants based on the specified criteria (rating descending, then ID descending). Finally, we extract the IDs from the sorted list and return them.

Step-by-Step Algorithm

  1. Step 1: Initialize an empty list `filtered_restaurants` to store the restaurants that pass the filter conditions.
  2. Step 2: Iterate through the `restaurants` list. For each restaurant, check if it satisfies all the given conditions: `veganFriendly`, `maxPrice`, and `maxDistance`.
  3. Step 3: If a restaurant satisfies all conditions, add it to the `filtered_restaurants` list.
  4. Step 4: Sort the `filtered_restaurants` list according to the problem's criteria. Sort by rating in descending order first, and if ratings are equal, sort by ID in descending order.
  5. Step 5: Create a list `result` to store the restaurant IDs.
  6. Step 6: Iterate through the sorted `filtered_restaurants` list and add the ID of each restaurant to the `result` list.
  7. Step 7: Return the `result` list.

Key Insights

  • Insight 1: The problem requires filtering based on multiple conditions, so efficient filtering is crucial.
  • Insight 2: Sorting is a key operation, and the sorting criteria involve two levels of comparison: rating and ID.
  • Insight 3: Representing the data as arrays allows for direct access and efficient comparison of restaurant attributes.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(n)

Topics

This problem involves: Array, Sorting.

Companies

Asked at: Yelp.