0% found this document useful (0 votes)
101 views1 page

Leet Code Practice

This C# code defines a method to filter a 2D array of restaurant data based on vegan friendliness, max price, and distance criteria. It adds matching restaurants to a filtered list, sorts the list based on a comparison method, and returns just the restaurant IDs from the sorted list.

Uploaded by

Maneesh Darisi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views1 page

Leet Code Practice

This C# code defines a method to filter a 2D array of restaurant data based on vegan friendliness, max price, and distance criteria. It adds matching restaurants to a filtered list, sorts the list based on a comparison method, and returns just the restaurant IDs from the sorted list.

Uploaded by

Maneesh Darisi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

public class Solution {

public IList<int> FilterRestaurants(int[][] restaurants, int veganFriendly,


int maxPrice, int maxDistance)
{
List<int[]> filtered = new List<int[]>();
List<int> ids = new List<int>();
foreach (var item in restaurants)
{
if (veganFriendly <= item[2] && maxPrice >= item[3] && maxDistance
>= item[4])
{
filtered.Add(item);
}
}
filtered.Sort(CompareArray);
foreach (var item in filtered)
{
ids.Add(item[0]);
}
return ids;
}
public int CompareArray(int[] x, int[] y)
{
if (x[1] > y[1])
{
return -1;
}
if (x[1] == y[1])
{
if (x[0] > y[0])
{
return -1;
}
else
{
return 1;
}
}
else
{
return 1;
}
}
}

You might also like