Skip to content

Commit 01ae12f

Browse files
Merge pull request #579 from anirudherabelly/master
added important queue algos
2 parents a809eb4 + 9db78f5 commit 01ae12f

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class GfG{
2+
public Queue<Integer> rev(Queue<Integer> q){
3+
//add code here.
4+
//revHelperRecursive(q);
5+
revHelperIterative(q);
6+
return q;
7+
}
8+
private void revHelperIterative(Queue<Integer> q){
9+
Stack<Integer> s=new Stack<Integer>();
10+
while(!q.isEmpty()){
11+
s.push(q.poll());
12+
}
13+
while(!s.empty()){
14+
q.offer(s.pop());
15+
}
16+
}
17+
private void revHelperRecursive(Queue<Integer> q){
18+
if(q.isEmpty())return;
19+
int temp=q.poll();
20+
revHelperRecursive(q);
21+
q.offer(temp);
22+
}
23+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
class PetrolPump
5+
{
6+
public int petrolGiven;
7+
public int distanceToNext;
8+
public PetrolPump(int PetrolGiven,int DistanceToNext)
9+
{
10+
this.petrolGiven = PetrolGiven;
11+
this.distanceToNext = DistanceToNext;
12+
}
13+
}
14+
class Solution {
15+
static void Main(String[] args) {
16+
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
17+
int n = int.Parse(Console.ReadLine());
18+
PetrolPump[] q = new PetrolPump[n];
19+
for (int i = 0; i < n; i++)
20+
{
21+
int[] info = Array.ConvertAll(Console.ReadLine().Split(' '),int.Parse);
22+
q[i]=new PetrolPump(info[0],info[1]);
23+
}
24+
TruckTour(q,n);
25+
}
26+
private static void TruckTour(PetrolPump[] q, int n)
27+
{
28+
int start = 0,end=1;
29+
int curr_petrol= q[start].petrolGiven- q[start].distanceToNext;
30+
while(curr_petrol<0 || start != end)
31+
{
32+
while(curr_petrol<0 && start != end)
33+
{
34+
curr_petrol -= (q[start].petrolGiven - q[start].distanceToNext);
35+
start = (start + 1) % n;
36+
if (start == 0)
37+
{
38+
Console.WriteLine("Not Possible");
39+
return;
40+
}
41+
}
42+
curr_petrol += q[end].petrolGiven - q[end].distanceToNext;
43+
end = (end + 1) % n;
44+
}
45+
Console.WriteLine(start);
46+
}
47+
}

0 commit comments

Comments
 (0)