From 696377d7cca7e95f18ba69232bccd4034c5dd882 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 7 Jul 2019 23:02:26 -0700 Subject: [PATCH] 1102 added. --- .../cpp-1102/CMakeLists.txt | 6 ++ .../cpp-1102/main.cpp | 73 +++++++++++++++++++ readme.md | 2 +- 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 1102-Path-With-Maximum-Minimum-Value/cpp-1102/CMakeLists.txt create mode 100644 1102-Path-With-Maximum-Minimum-Value/cpp-1102/main.cpp diff --git a/1102-Path-With-Maximum-Minimum-Value/cpp-1102/CMakeLists.txt b/1102-Path-With-Maximum-Minimum-Value/cpp-1102/CMakeLists.txt new file mode 100644 index 00000000..89658265 --- /dev/null +++ b/1102-Path-With-Maximum-Minimum-Value/cpp-1102/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.14) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main.cpp) \ No newline at end of file diff --git a/1102-Path-With-Maximum-Minimum-Value/cpp-1102/main.cpp b/1102-Path-With-Maximum-Minimum-Value/cpp-1102/main.cpp new file mode 100644 index 00000000..95c4b18a --- /dev/null +++ b/1102-Path-With-Maximum-Minimum-Value/cpp-1102/main.cpp @@ -0,0 +1,73 @@ +/// Source : https://leetcode.com/problems/path-with-maximum-minimum-value/ +/// Author : liuyubobobo +/// Time : 2019-06-30 + +#include +#include +#include + +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>& 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>& 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 q; + q.push(0); + + vector 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; +} \ No newline at end of file diff --git a/readme.md b/readme.md index c6144945..c21facbc 100644 --- a/readme.md +++ b/readme.md @@ -751,7 +751,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 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/) | [无]
[缺:并查集] | [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/) | | |