Skip to content

Commit

Permalink
1102 added.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Jul 8, 2019
1 parent fbdf39f commit 696377d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
6 changes: 6 additions & 0 deletions 1102-Path-With-Maximum-Minimum-Value/cpp-1102/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.14)
project(D)

set(CMAKE_CXX_STANDARD 14)

add_executable(D main.cpp)
73 changes: 73 additions & 0 deletions 1102-Path-With-Maximum-Minimum-Value/cpp-1102/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/// Source : https://leetcode.com/problems/path-with-maximum-minimum-value/
/// Author : liuyubobobo
/// Time : 2019-06-30

#include <iostream>
#include <vector>
#include <queue>

using namespace std;


/// Binary Search
/// Time Complexity: O(log(max(A)) * m * n)
/// Space Complexity: O(1)
class Solution {

private:
int m, n;

public:
int maximumMinimumPath(vector<vector<int>>& A) {

m = A.size(), n = A[0].size();

int min_value = A[0][0], max_value = A[0][0];
for(int i = 0; i < m; i ++)
for(int j = 0; j < n; j ++)
min_value = min(min_value, A[i][j]),
max_value = max(max_value, A[i][j]);

int l = min_value, r = max_value;
while(l < r){
int mid = l + (r - l + 1) / 2;
if(ok(A, mid)) l = mid;
else r = mid - 1;
}
return l;
}

private:
bool ok(const vector<vector<int>>& A, int K){

if(A[0][0] < K || A[m - 1][n - 1] < K) return false;

const int d[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
queue<int> q;
q.push(0);

vector<bool> visited(m * n, false);
visited[0] = true;

while(!q.empty()){
int cx = q.front() / n, cy = q.front() % n;
q.pop();

for(int i = 0; i < 4; i ++){
int nx = cx + d[i][0], ny = cy + d[i][1];
int index = nx * n + ny;
if(nx >= 0 && nx < m && ny >= 0 && ny < n && A[nx][ny] >= K && !visited[index]){
visited[index] = true;
q.push(index);
}
}
}
return visited.back();
}
};


int main() {

return 0;
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ email: [[email protected]](mailto:[email protected])
| 1099 | [Two Sum Less Than K](https://leetcode.com/problems/two-sum-less-than-k/) | [] | [C++](1099-Two-Sum-Less-Than-K/cpp-1099/) | | |
| 1100 | [Find K-Length Substrings With No Repeated Characters](https://leetcode.com/problems/find-k-length-substrings-with-no-repeated-characters/) | [] | [C++](1100-Find-K-Length-Substrings-With-No-Repeated-Characters/cpp-1100/) | | |
| 1101 | [The Earliest Moment When Everyone Become Friends](https://leetcode.com/problems/the-earliest-moment-when-everyone-become-friends/) | [] | [C++](1101-The-Earliest-Moment-When-Everyone-Become-Friends/cpp-1101/) | | |
| | | | | | |
| 1102 | [Path With Maximum Minimum Value](https://leetcode.com/problems/path-with-maximum-minimum-value/) | []<br/>[缺:并查集] | [C++](1102-Path-With-Maximum-Minimum-Value/cpp-1102/) | | |
| 1103 | [Distribute Candies to People](https://leetcode.com/problems/distribute-candies-to-people/) | [solution](https://leetcode.com/problems/distribute-candies-to-people/solution/) | [C++](1103-Distribute-Candies-to-People/cpp-1103/) | | |
| 1104 | [Path In Zigzag Labelled Binary Tree](https://leetcode.com/problems/path-in-zigzag-labelled-binary-tree/) | [] | [C++](1104-Path-In-Zigzag-Labelled-Binary-Tree/cpp-1104/) | | |
| 1105 | [Filling Bookcase Shelves](https://leetcode.com/problems/filling-bookcase-shelves/) | [] | [C++](1105-Filling-Bookcase-Shelves/cpp-1105/) | | |
Expand Down

0 comments on commit 696377d

Please sign in to comment.