Skip to content

Commit

Permalink
Sync LeetCode submission Runtime - 207 ms (17.22%), Memory - 73.1 MB …
Browse files Browse the repository at this point in the history
…(7.15%)
  • Loading branch information
pradeeptosarkar committed May 15, 2024
1 parent 189fede commit 455cc6e
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
45 changes: 45 additions & 0 deletions LeetCode/1753-path-with-minimum-effort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<p>You are a hiker preparing for an upcoming hike. You are given <code>heights</code>, a 2D array of size <code>rows x columns</code>, where <code>heights[row][col]</code> represents the height of cell <code>(row, col)</code>. You are situated in the top-left cell, <code>(0, 0)</code>, and you hope to travel to the bottom-right cell, <code>(rows-1, columns-1)</code> (i.e.,&nbsp;<strong>0-indexed</strong>). You can move <strong>up</strong>, <strong>down</strong>, <strong>left</strong>, or <strong>right</strong>, and you wish to find a route that requires the minimum <strong>effort</strong>.</p>

<p>A route&#39;s <strong>effort</strong> is the <strong>maximum absolute difference</strong><strong> </strong>in heights between two consecutive cells of the route.</p>

<p>Return <em>the minimum <strong>effort</strong> required to travel from the top-left cell to the bottom-right cell.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2020/10/04/ex1.png" style="width: 300px; height: 300px;" /></p>

<pre>
<strong>Input:</strong> heights = [[1,2,2],[3,8,2],[5,3,5]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The route of [1,3,5,3,5] has a maximum absolute difference of 2 in consecutive cells.
This is better than the route of [1,2,2,2,5], where the maximum absolute difference is 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2020/10/04/ex2.png" style="width: 300px; height: 300px;" /></p>

<pre>
<strong>Input:</strong> heights = [[1,2,3],[3,8,4],[5,3,5]]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The route of [1,2,3,4,5] has a maximum absolute difference of 1 in consecutive cells, which is better than route [1,3,5,3,5].
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/04/ex3.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> heights = [[1,2,1,1,1],[1,2,1,2,1],[1,2,1,2,1],[1,2,1,2,1],[1,1,1,2,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> This route does not require any effort.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>rows == heights.length</code></li>
<li><code>columns == heights[i].length</code></li>
<li><code>1 &lt;= rows, columns &lt;= 100</code></li>
<li><code>1 &lt;= heights[i][j] &lt;= 10<sup>6</sup></code></li>
</ul>
65 changes: 65 additions & 0 deletions LeetCode/1753-path-with-minimum-effort/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class Solution {
private:
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
public:
int minimumEffortPath(vector<vector<int>>& heights) {

int low=0;
int high=1e6;
int ans=0;
while(low<=high)
{
int mid=(low+high)/2;

if(check(heights,mid))
{
ans=mid;
high=mid-1;
}
else
{
low=mid+1;
}
}
return ans;

}

bool check(vector<vector<int>>&heights,int mid)
{
int r=heights.size();
int c=heights[0].size();
int i,j;

vector<vector<int>>vis(r,vector<int>(c,0));
vis[0][0]=1;
queue<pair<int,int>>q;
q.push({0,0});
while(!q.empty())
{
int x=q.front().first;
int y=q.front().second;
q.pop();
if(x==r-1 and y==c-1)
return true;
for(int k=0;k<4;k++)
{
int nx=dx[k]+x;
int ny=dy[k]+y;
if(nx>=0 and ny>=0 and nx<r and ny<c and !vis[nx][ny])
{
int newDistance=abs(heights[nx][ny]-heights[x][y]);

if(newDistance<=mid)
{
q.push({nx,ny});
vis[nx][ny]=1;
}

}
}
}
return false;
}
};

0 comments on commit 455cc6e

Please sign in to comment.