Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0a9d7ca

Browse files
added Max with Min Altitudes
1 parent 007113e commit 0a9d7ca

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Max Of Min Altitudes
2+
3+
[Leetcode Link](https://leetcode.com/discuss/interview-question/383669/)
4+
5+
## Problem:
6+
7+
Given a matrix with r rows and c columns, find the maximum score of a path starting at [0, 0] and ending at [r-1, c-1]. The score of a path is the minimum value in that path. For example, the score of the path 8 → 4 → 5 → 9 is 4.
8+
9+
Don't include the first or final entry. You can only move either down or right at any point in time.
10+
11+
## Example:
12+
13+
```
14+
Input:
15+
[[5, 1],
16+
[4, 5]]
17+
18+
Output: 4
19+
Explanation:
20+
Possible paths:
21+
5 → 1 → 5 => min value is 1
22+
5 → 4 → 5 => min value is 4
23+
Return the max value among minimum values => max(4, 1) = 4.
24+
```
25+
26+
```
27+
Input:
28+
[[1, 2, 3]
29+
[4, 5, 1]]
30+
31+
Output: 4
32+
Explanation:
33+
Possible paths:
34+
1-> 2 -> 3 -> 1
35+
1-> 2 -> 5 -> 1
36+
1-> 4 -> 5 -> 1
37+
So min of all the paths = [2, 2, 4]. Note that we don't include the first and final entry.
38+
Return the max of that, so 4.
39+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def maxScore(grid):
2+
dp = [[None for _ in range(len(grid))] for _ in range(len(grid))]
3+
# top and left borders have 1 unique path, so the score is minimum of previous col or row
4+
for j in range(1, len(dp[0])):
5+
dp[0][j] = grid[0][j] if j == 1 else min(dp[0][j-1], grid[0][j])
6+
for i in range(1, len(dp)):
7+
dp[i][0] = grid[i][0] if i == 1 else min(dp[i-1][0], grid[i][0])
8+
# score will be based on either route is from top or left, we want to take max score out of two paths
9+
for i in range(1, len(dp)):
10+
for j in range(1, len(dp[i])):
11+
fromTop = min(dp[i-1][j], grid[i][j])
12+
fromLeft = min(dp[i][j-1], grid[i][j])
13+
dp[i][j] = max(fromTop, fromLeft)
14+
return max(dp[-2][-1], dp[-1][-2])
15+
16+
17+
print(maxScore([[1, 2, 3], [4, 5, 1]]))
18+
print(maxScore([[8, 4, 5], [3, 2, 7], [1, 6, 9]]))

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Languages used: Java and Python
6363
- [Longest Increasing Subsequence](DynamicProgramming/LongestIncreasingSubsequence)
6464
- [Longest Palindromic Substring](DynamicProgramming/LongestPalindromicSubstring)
6565
- [Maximum Length Pair Chain](DynamicProgramming/MaximumLengthPairChain)
66+
- [Max with Min Altitudes](DynamicProgramming/MaxWithMinAltitudes)
6667
- [Number Of Islands](DynamicProgramming/NumberOfIslands)
6768
- [Palindromic Substrings](DynamicProgramming/PalindromicSubstrings)
6869
- [Predict Winner](DynamicProgramming/PredictWinner)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /