Skip to content

Commit 113b9ef

Browse files
Merge pull request #135 from DevendraJaiswal074/Leetcode_Program
Path with Maximum Probability
2 parents ba2787a + f85dc23 commit 113b9ef

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
You are given an undirected weighted graph of n nodes (0-indexed), represented by an edge list where edges[i] = [a, b] is an undirected edge connecting the nodes a and b with a probability of success of traversing that edge succProb[i].
3+
4+
Given two nodes start and end, find the path with the maximum probability of success to go from start to end and return its success probability.
5+
6+
If there is no path from start to end, return 0. Your answer will be accepted if it differs from the correct answer by at most 1e-5.
7+
8+
*/
9+
10+
class Solution {
11+
public double maxProbability(int n, int[][] edges, double[] succProb, int start_node, int end_node) {
12+
double[] maxProb = new double[n];
13+
maxProb[start_node] = 1.0;
14+
for (int i = 0; i < n - 1; i++) {
15+
boolean updated = false;
16+
for (int j = 0; j < edges.length; j++) {
17+
int u = edges[j][0];
18+
int v = edges[j][1];
19+
double prob = succProb[j];
20+
21+
if (maxProb[u] * prob > maxProb[v]) {
22+
maxProb[v] = maxProb[u] * prob;
23+
updated = true;
24+
}
25+
if (maxProb[v] * prob > maxProb[u]) {
26+
maxProb[u] = maxProb[v] * prob;
27+
vs updated = true;
28+
}
29+
}
30+
if (!updated) break;
31+
}
32+
return maxProb[end_node];
33+
}
34+
}
35+
36+
/*
37+
38+
Example 1:
39+
40+
Input: n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.2], start = 0, end = 2
41+
Output: 0.25000
42+
*/
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
//https://leetcode.com/problems/walking-robot-simulation/
3+
4+
//874. Walking Robot Simulation
5+
6+
7+
class Solution {
8+
public int robotSim(int[] commands, int[][] obstacles) {
9+
// Directions: north, east, south, west
10+
int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
11+
int x = 0, y = 0; // Robot's starting position
12+
int dir = 0; // Start facing north
13+
int maxDistSquared = 0; // Maximum distance squared from the origin
14+
15+
// Store obstacles as a set of strings "x,y"
16+
Set<String> obstacleSet = new HashSet<>();
17+
for (int[] obstacle : obstacles) {
18+
obstacleSet.add(obstacle[0] + "," + obstacle[1]);
19+
}
20+
21+
// Process each command
22+
for (int command : commands) {
23+
if (command == -2) { // Turn left
24+
dir = (dir + 3) % 4; // Equivalent to turning left 90 degrees
25+
} else if (command == -1) { // Turn right
26+
dir = (dir + 1) % 4; // Equivalent to turning right 90 degrees
27+
} else { // Move forward
28+
for (int i = 0; i < command; i++) {
29+
int nextX = x + directions[dir][0];
30+
int nextY = y + directions[dir][1];
31+
// Check if the next position is an obstacle
32+
if (!obstacleSet.contains(nextX + "," + nextY)) {
33+
x = nextX;
34+
y = nextY;
35+
// Update the maximum distance squared
36+
maxDistSquared = Math.max(maxDistSquared, x * x + y * y);
37+
} else {
38+
// Stop moving if there's an obstacle
39+
break;
40+
}
41+
}
42+
}
43+
}
44+
45+
return maxDistSquared;
46+
}
47+
}
48+
49+
/*
50+
Example 1:
51+
52+
Input: commands = [4,-1,3], obstacles = []
53+
54+
Output: 25
55+
56+
Explanation:
57+
58+
The robot starts at (0, 0):
59+
60+
Move north 4 units to (0, 4).
61+
Turn right.
62+
Move east 3 units to (3, 4).
63+
The furthest point the robot ever gets from the origin is (3, 4), which squared is 32 + 42 = 25 units away.
64+
*/

0 commit comments

Comments
 (0)