Skip to content

Commit 90e854c

Browse files
authored
Adding the solution of Path with Minimum Effort (#123)
1 parent 1a4de52 commit 90e854c

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

Java/path-with-minimum-effort.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* Problem Name : Path with Minimum Effort
3+
* Concept Involved : Graphs, Dijkstra's Shortest Path
4+
*
5+
* Execution Time : 1229 ms
6+
* Memory Consumed : 39.4 mb
7+
*
8+
*/
9+
class Point{
10+
int x;
11+
int y;
12+
int height;
13+
14+
public Point(int x, int y, int height){
15+
this.x = x;
16+
this.y = y;
17+
this.height = height;
18+
}
19+
}
20+
class Solution {
21+
public int minimumEffortPath(int[][] heights) {
22+
int n = heights.length;
23+
int m = heights[0].length;
24+
25+
int sx = 0;
26+
int sy = 0;
27+
28+
int[][] dist = new int[n][m];
29+
int[][] vis = new int[n][m];
30+
31+
for(int i=0; i<n; i++){
32+
for(int j=0; j<m; j++){
33+
dist[i][j] = Integer.MAX_VALUE;
34+
}
35+
}
36+
dist[sx][sy] = 0;
37+
38+
for(int i=0; i<n; i++){
39+
for(int j=0; j<m; j++){
40+
int px = -1;
41+
int py = -1;
42+
int mdist = Integer.MAX_VALUE;
43+
44+
for(int k=0; k<n; k++){
45+
for(int l=0; l<m; l++){
46+
if(mdist > dist[k][l] && vis[k][l]==0){
47+
mdist = dist[k][l];
48+
px = k;
49+
py = l;
50+
}
51+
}
52+
}
53+
54+
vis[px][py] = 1;
55+
Point cpoint = new Point(px, py, heights[px][py]);
56+
57+
if(cpoint.x > 0){
58+
int lx = cpoint.x - 1;
59+
int ly = cpoint.y;
60+
if(vis[lx][ly] == 0){
61+
dist[lx][ly] = Math.min(dist[lx][ly], Math.max(dist[cpoint.x][cpoint.y], Math.abs(cpoint.height - heights[lx][ly])));
62+
}
63+
}
64+
if(cpoint.x < n-1){
65+
int lx = cpoint.x + 1;
66+
int ly = cpoint.y;
67+
if(vis[lx][ly] == 0){
68+
dist[lx][ly] = Math.min(dist[lx][ly], Math.max(dist[cpoint.x][cpoint.y], Math.abs(cpoint.height - heights[lx][ly])));
69+
}
70+
}
71+
if(cpoint.y > 0){
72+
int lx = cpoint.x;
73+
int ly = cpoint.y - 1;
74+
if(vis[lx][ly] == 0){
75+
dist[lx][ly] = Math.min(dist[lx][ly], Math.max(dist[cpoint.x][cpoint.y], Math.abs(cpoint.height - heights[lx][ly])));
76+
}
77+
}
78+
if(cpoint.y < m-1){
79+
int lx = cpoint.x;
80+
int ly = cpoint.y + 1;
81+
if(vis[lx][ly] == 0){
82+
dist[lx][ly] = Math.min(dist[lx][ly], Math.max(dist[cpoint.x][cpoint.y], Math.abs(cpoint.height - heights[lx][ly])));
83+
}
84+
}
85+
}
86+
}
87+
88+
return dist[n-1][m-1];
89+
}
90+
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
373373
| 947 | [Most Stones Removed with Same Row or Column](https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/) | [C++](./C++/Most-Stones-Removed-with-Same-Row-or-Column.cpp) | _O(V)_ | _O(2V)_ | Medium | Graph | Union Find | | [C++](./C++/Most-Stones-Removed-with-Same-Row-or-Column.cpp) | _O(V)_ | _O(2V)_ | Medium | Graph | Union Find |
374374
| 210 | [Course Schedule II](https://leetcode.com/problems/course-schedule-ii/) | [C++](./C++/Course-Schedule-II.cpp) | _O(V+E)_ | _O(V)_ | Medium | Graph | BFS |
375375
| 1627 | [Graph Connectivity with Threshold](https://leetcode.com/problems/graph-connectivity-with-threshold/) | [Java](./Java/graph_connectivity_with_threshold.java) | _O(V.logV + Q)_ | _O(V)_ | Hard | Graph | Union Find + Sieve |
376-
376+
| 1631 | [Path with Minimum Effort](https://leetcode.com/problems/path-with-minimum-effort/) | [Java](./Java/path-with-minimum-effort.java) | _O(V^2)_ | _O(V)_ | Medium | Graph | Dijkstra's Shortest Path |
377377
<br/>
378378
<div align="right">
379379
<b><a href="#algorithms">⬆️ Back to Top</a></b>
@@ -437,7 +437,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
437437
| [James Y](https://github.com/jameszu) <br> <img src="https://avatars0.githubusercontent.com/u/41566813?s=400&u=af77d15517566ea590a316030b4a6d402b0041b6&v=4" width="100" height="100"> | New Zealand | python | [Github](https://github.com/jameszu) |
438438
| [Hamza B](https://github.com/9Hamza) <br> <img src="https://avatars2.githubusercontent.com/u/56516922?s=400&u=2c1adeef0194a2859361d464f28783bfba698638&v=4" width="100" height="100"> | Saudi Arabia | Java | [Github](https://github.com/9Hamza) |
439439
| [Meli Haktas](https://github.com/MercerFrey) <br> <img src="https://avatars1.githubusercontent.com/u/29127873?s=460&u=149319db4468ec2316e49a75eb5e05b35eb05eef&v=4" width="100" height="100"> | Turkey | python | [Github](https://github.com/MercerFrey) |
440-
| [Saurav Prateek](https://github.com/SauravP97) <br> <img src="https://avatars3.githubusercontent.com/u/26816418?s=460&u=4b5222d5b6db79389efba062714c9dfba263732f&v=4" width="100" height="100"> | India | Java | [Github](https://github.com/SauravP97) |
440+
| [Saurav Prateek](https://github.com/SauravP97) <br> <img src="https://avatars3.githubusercontent.com/u/26816418?s=460&u=4b5222d5b6db79389efba062714c9dfba263732f&v=4" width="100" height="100"> | India | Java | [Github](https://github.com/SauravP97) <br> [Codechef](https://www.codechef.com/users/srvptk) <br> [Codeforces](https://codeforces.com/profile/srvptk97) <br> [Leetcode](https://leetcode.com/srvptk97) | |
441441

442442
<br/>
443443
<div align="right">

0 commit comments

Comments
 (0)