Given max travel distance, forward and backwards routes, return the routes which utilizes maximum travel distance.
Example:
Forward route : [[1,3000],[2,5000],[3,4000],[4,10000],[5,8000]]
Backward route : [[1,1000],[2,3000],[3,4000]]
Max Distance Traveled: 11000
Result must be: [4,1] and [5,2] as the total distance traveled is 11000 which is less than or equal to max distance.
Code:
static void Main(string[] args)
{
int[][] f = new int[5][];
int[][] b = new int[3][];
f[0] = new int[] { 1, 3000 };
f[1] = new int[] { 2, 5000 };
f[2] = new int[] { 3, 4000 };
f[3] = new int[] { 4, 10000 };
f[4] = new int[] { 5, 8000 };
b[0] = new int[] { 1, 1000 };
b[1] = new int[] { 2, 3000 };
b[2] = new int[] { 3, 4000 };
var result = sol(f, b, 11000);
}
public static List<List<int>> sol(int[][] f, int[][] b,int max)
{
List<List<int>> li = new List<List<int>>();
int m = 0;
for (int i = 0; i < f.Length; i++)
{
for (int j = 0; j < b.Length; j++)
{
if (f[i][1] + b[j][1] <= max)
{
li.Add(new List<int>() { f[i][0], b[j][0], f[i][1] + b[j][1] });
if (m < f[i][1] + b[j][1])
{
m = f[i][1] + b[j][1];
}
}
}
}
return li.Where(i => i[2] == m).ToList();
}
Popular Posts
-
Here is the code: // => Brute Force Solution: O(m* n) static long arrayManipulation(int n, int[][] queries) { ...
-
Whatever it is one fine day everyone on this planet who are born have to die for sure. When you are close ones are with you, you wont know ...
-
HTML: <div class="outer-container"> <div class="inner-container"> <div class="t...
No comments:
Post a Comment