-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1102.path-with-maximum-minimum-value.cpp
More file actions
129 lines (123 loc) · 3.83 KB
/
1102.path-with-maximum-minimum-value.cpp
File metadata and controls
129 lines (123 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Given a matrix of integers A 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
// value of the path 8 → 4 → 5 → 9 is 4.
//
// A path moves some number of times from one visited cell to any neighbouring
// unvisited cell in one of the 4 cardinal directions (north, east, west,
// south).
//
//
//
// --------------------------------------------------
//
// Example 1:
//
//
//
//
// Input: [[5,4,5],[1,2,6],[7,4,6]]
// Output: 4
// Explanation:
// The path with the maximum score is highlighted in yellow.
//
//
// --------------------------------------------------
//
// Example 2:
//
//
//
//
// Input: [[2,2,1,2,2,2],[1,2,2,2,1,2]]
// Output: 2
//
// --------------------------------------------------
//
// Example 3:
//
//
//
//
// Input:
// [[3,4,6,3,4],[0,2,1,1,7],[8,8,3,2,7],[3,2,4,9,8],[4,1,2,0,0],[4,6,5,4,3]]
// Output: 3
//
//
//
// * * * * * * * * * * * * * * * * * * * * * * * * *
//
// Note:
//
//
// 1 <= R, C <= 100
// 0 <= A[i][j] <= 10^9
//
//
class Solution {
public:
std::vector<std::vector<int>> neighbour_basis{
{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
enum state { UNVISITED, VISITED };
inline std::vector<std::vector<int>> findNeighbours(
int row, int col, std::vector<std::vector<int>>& A,
std::vector<std::vector<state>>& states) {
std::vector<std::vector<int>> neighbours;
for (int i = 0; i < 4; ++i) {
int new_row = row + neighbour_basis[i][0];
int new_col = col + neighbour_basis[i][1];
if (new_row >= 0 && new_row < A.size() && new_col >= 0 &&
new_col < A[new_row].size() &&
states[new_row][new_col] == UNVISITED) {
neighbours.push_back(std::vector<int>{new_row, new_col});
}
}
return neighbours;
}
struct CompareScore {
bool operator()(const std::vector<int>& a, const std::vector<int>& b) {
return a[2] < b[2];
}
};
int maximumMinimumPath(std::vector<std::vector<int>>& A) {
// we use djikstras algorithm. to find the single source shortest path
std::vector<std::vector<int>> scores(A.size(),
std::vector<int>(A[0].size(), -1));
std::priority_queue<std::vector<int>> queue;
std::vector<std::vector<state>> states(
A.size(), std::vector<state>(A[0].size(), UNVISITED));
queue.push(std::vector<int>{0, 0, A[0][0]});
while (!queue.empty()) {
std::vector<int> current_node = queue.top();
queue.pop();
int row = current_node[0];
int col = current_node[1];
int score = current_node[2];
// if the node obtained shows that the score is lesser than the recorded
// score dont try to compute its neighbours scores, because we have
// already done it with a better recorded score, since better scores are
// popped from the priority queue first
if (score <= scores[row][col]) {
continue;
}
scores[row][col] = score;
if (row == A.size() - 1 && col == A[0].size() - 1) {
break;
}
std::vector<std::vector<int>> neighbours =
findNeighbours(row, col, A, states);
for (int i = 0; i < neighbours.size(); ++i) {
// for all neighbours calculate the score of getting to them and update
// the score table
int neighbour_row = neighbours[i][0];
int neighbour_col = neighbours[i][1];
int neighbour_score = std::min(score, A[neighbour_row][neighbour_col]);
states[neighbour_row][neighbour_col] = VISITED;
queue.push(
std::vector<int>{neighbour_row, neighbour_col, neighbour_score});
}
}
return scores[A.size() - 1][A[0].size() - 1];
}
};