Skip to content

Commit 4773038

Browse files
authored
Add solution for leetcode's CheapKFlights
1 parent 83213ff commit 4773038

File tree

1 file changed

+177
-0
lines changed

1 file changed

+177
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
package com.mckinsey.engage.risk;
2+
3+
//public class CheapKFlights {
4+
// public static void main(String[] args) {
5+
// int n = 3;
6+
// int[][] edges = {{0, 1, 100}, {1, 2, 100}, {0, 2, 500}};
7+
// int src = 0;
8+
// int dst = 2;
9+
// int k = 1;
10+
//
11+
// Map<Integer, List<NodeWithCost>> graph = convertMatrixToList(edges);
12+
//
13+
// int cheapestPrice = getCheapestPrice(src, dst, k, graph, new HashSet<>(),
14+
// 0, 0);
15+
//
16+
// System.out.println(cheapestPrice);
17+
// }
18+
//
19+
// static int getCheapestPrice(int src, int dst, int allowedStops, Map<Integer,
20+
// List<NodeWithCost>> graph, Set<Integer> visited, int currentStops, int cost) {
21+
//
22+
// if (src == dst) {
23+
// currentStops--;
24+
// return cost;
25+
// }
26+
//
27+
// if ((currentStops > allowedStops) || visited.contains(src))
28+
// return Integer.MAX_VALUE;
29+
//
30+
// visited.add(src);
31+
//
32+
// List<NodeWithCost> neighboursWithCost = graph.get(src);
33+
// int cheapestPrice = Integer.MAX_VALUE;
34+
//
35+
// for (NodeWithCost neighbourWithCost : neighboursWithCost) {
36+
// cheapestPrice = Math.min(cheapestPrice, getCheapestPrice(neighbourWithCost.getNode(),
37+
// dst, allowedStops, graph, visited, currentStops + 1,
38+
// cost + neighbourWithCost.getCost()));
39+
// }
40+
//
41+
// return cheapestPrice;
42+
// }
43+
//
44+
//
45+
// static Map<Integer, List<NodeWithCost>> convertMatrixToList(int[][] edges) {
46+
// Map<Integer, List<NodeWithCost>> graph = new HashMap<>();
47+
//
48+
// for (int[] edge : edges) {
49+
// if (graph.containsKey(edge[0])) {
50+
// graph.get(edge[0]).add(new NodeWithCost(edge[1], edge[2]));
51+
// } else {
52+
// LinkedList<NodeWithCost> neighboursWithCost = new LinkedList<>();
53+
// neighboursWithCost.add(new NodeWithCost(edge[1], edge[2]));
54+
// graph.put(edge[0], neighboursWithCost);
55+
// }
56+
// }
57+
//
58+
// return graph;
59+
// }
60+
//}
61+
//
62+
//class NodeWithCost {
63+
// Integer node;
64+
// Integer Cost;
65+
//
66+
// public NodeWithCost(Integer node, Integer cost) {
67+
// this.node = node;
68+
// Cost = cost;
69+
// }
70+
//
71+
// public Integer getNode() {
72+
// return node;
73+
// }
74+
//
75+
// public Integer getCost() {
76+
// return Cost;
77+
// }
78+
//}
79+
80+
81+
82+
83+
/*
84+
85+
0-->3
86+
| |
87+
V V
88+
4-->1-->2
89+
A |
90+
| |
91+
---V
92+
93+
94+
*/
95+
96+
97+
import java.util.*;
98+
99+
class CheapKFlights {
100+
public static void main(String s[]) {
101+
int n = 5;
102+
int[][] flights = {{4, 1, 1}, {1, 2, 3}, {0, 3, 2}, {0, 4, 10}, {3, 1, 1}, {1, 4, 3}};
103+
int src = 2;
104+
int dst = 1;
105+
int K = 1;
106+
107+
Map<Integer, List<NodeWithCost>> graph = convertMatrixToList(flights);
108+
109+
int cheapestPrice = getCheapestPrice(src, dst, K, graph, new HashSet<>(), 0, 0);
110+
111+
System.out.println(cheapestPrice);
112+
}
113+
114+
static int getCheapestPrice(int src, int dst, int allowedStops, Map<Integer,
115+
List<NodeWithCost>> graph, Set<Integer> visited, int currentStops, int cost) {
116+
117+
if ((currentStops > allowedStops) || visited.contains(src))
118+
return Integer.MAX_VALUE;
119+
120+
if (src == dst) {
121+
currentStops--;
122+
return cost;
123+
}
124+
125+
visited.add(src);
126+
127+
List<NodeWithCost> neighboursWithCost = graph.get(src);
128+
int cheapestPrice = Integer.MAX_VALUE;
129+
130+
if (neighboursWithCost != null) {
131+
for (NodeWithCost neighbourWithCost : neighboursWithCost) {
132+
cheapestPrice = Math.min(cheapestPrice, getCheapestPrice(neighbourWithCost.getNode(),
133+
dst, allowedStops, graph, visited, currentStops + 1,
134+
cost + neighbourWithCost.getCost()));
135+
}
136+
visited.remove(src);
137+
} else {
138+
return 0;
139+
}
140+
141+
return cheapestPrice;
142+
}
143+
144+
static Map<Integer, List<NodeWithCost>> convertMatrixToList(int[][] edges) {
145+
Map<Integer, List<NodeWithCost>> graph = new HashMap<>();
146+
147+
for (int[] edge : edges) {
148+
if (graph.containsKey(edge[0])) {
149+
graph.get(edge[0]).add(new NodeWithCost(edge[1], edge[2]));
150+
} else {
151+
List<NodeWithCost> neighboursWithCost = new LinkedList<>();
152+
neighboursWithCost.add(new NodeWithCost(edge[1], edge[2]));
153+
graph.put(edge[0], neighboursWithCost);
154+
}
155+
}
156+
157+
return graph;
158+
}
159+
160+
private static class NodeWithCost {
161+
Integer node;
162+
Integer cost;
163+
164+
public NodeWithCost(Integer node, Integer cost) {
165+
this.node = node;
166+
this.cost = cost;
167+
}
168+
169+
public Integer getNode() {
170+
return node;
171+
}
172+
173+
public Integer getCost() {
174+
return cost;
175+
}
176+
}
177+
}

0 commit comments

Comments
 (0)