Skip to content

Commit

Permalink
Merge pull request #135 from DevendraJaiswal074/Leetcode_Program
Browse files Browse the repository at this point in the history
Path with Maximum Probability
  • Loading branch information
abhisek247767 authored Oct 31, 2024
2 parents ba2787a + f85dc23 commit 113b9ef
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Java/1514-Path with Maximum Probability.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
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].
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.
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.
*/

class Solution {
public double maxProbability(int n, int[][] edges, double[] succProb, int start_node, int end_node) {
double[] maxProb = new double[n];
maxProb[start_node] = 1.0;
for (int i = 0; i < n - 1; i++) {
boolean updated = false;
for (int j = 0; j < edges.length; j++) {
int u = edges[j][0];
int v = edges[j][1];
double prob = succProb[j];

if (maxProb[u] * prob > maxProb[v]) {
maxProb[v] = maxProb[u] * prob;
updated = true;
}
if (maxProb[v] * prob > maxProb[u]) {
maxProb[u] = maxProb[v] * prob;
vs updated = true;
}
}
if (!updated) break;
}
return maxProb[end_node];
}
}

/*
Example 1:
Input: n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.2], start = 0, end = 2
Output: 0.25000
*/
64 changes: 64 additions & 0 deletions Java/874-Walking Robot Simulation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

//https://leetcode.com/problems/walking-robot-simulation/

//874. Walking Robot Simulation


class Solution {
public int robotSim(int[] commands, int[][] obstacles) {
// Directions: north, east, south, west
int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int x = 0, y = 0; // Robot's starting position
int dir = 0; // Start facing north
int maxDistSquared = 0; // Maximum distance squared from the origin

// Store obstacles as a set of strings "x,y"
Set<String> obstacleSet = new HashSet<>();
for (int[] obstacle : obstacles) {
obstacleSet.add(obstacle[0] + "," + obstacle[1]);
}

// Process each command
for (int command : commands) {
if (command == -2) { // Turn left
dir = (dir + 3) % 4; // Equivalent to turning left 90 degrees
} else if (command == -1) { // Turn right
dir = (dir + 1) % 4; // Equivalent to turning right 90 degrees
} else { // Move forward
for (int i = 0; i < command; i++) {
int nextX = x + directions[dir][0];
int nextY = y + directions[dir][1];
// Check if the next position is an obstacle
if (!obstacleSet.contains(nextX + "," + nextY)) {
x = nextX;
y = nextY;
// Update the maximum distance squared
maxDistSquared = Math.max(maxDistSquared, x * x + y * y);
} else {
// Stop moving if there's an obstacle
break;
}
}
}
}

return maxDistSquared;
}
}

/*
Example 1:
Input: commands = [4,-1,3], obstacles = []
Output: 25
Explanation:
The robot starts at (0, 0):
Move north 4 units to (0, 4).
Turn right.
Move east 3 units to (3, 4).
The furthest point the robot ever gets from the origin is (3, 4), which squared is 32 + 42 = 25 units away.
*/

0 comments on commit 113b9ef

Please sign in to comment.