leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 6867f0bdda4aa21c7e4cea5c9e9e1ccae5f7da14
parent c0d745cf4ad9d43b0a2fae08ee9c271d48e69755
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Thu, 26 Sep 2024 16:25:37 +0200

1 Random Problem

Diffstat:
AProblems/1901.cpp | 26++++++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 27 insertions(+), 0 deletions(-)

diff --git a/Problems/1901.cpp b/Problems/1901.cpp @@ -0,0 +1,26 @@ +class Solution { + public: + vector<int> findPeakGrid(const vector<vector<int>> &mat) const { + int beg = 0, end = mat[0].size() - 1; + + while (beg <= end) { + int maxi = 0, mid = beg + (end - beg) / 2; + + for (int i = 0; i < mat.size(); i++) { + maxi = mat[i][mid] >= mat[maxi][mid] ? i : maxi; + } + + bool isLeft = mid - 1 >= beg && mat[maxi][mid - 1] > mat[maxi][mid]; + bool isRight = mid + 1 <= end && mat[maxi][mid + 1] > mat[maxi][mid]; + + if (!isLeft && !isRight) + return {maxi, mid}; + else if (isRight) + beg = mid + 1; + else + end = mid - 1; + } + + return {-1, -1}; + } +}; diff --git a/README.md b/README.md @@ -1039,6 +1039,7 @@ for solving problems. | 1895 | Medium | [Largest Magic Square](Problems/1895.cpp) | | 1897 | Easy | [Redistribute Characters to Make All Strings Equal](Problems/1897.cpp) | | 1899 | Medium | [Merge Triplets to Form Target Triplet](Problems/1899.cpp) | +| 1901 | Medium | [Find a Peak Element II](Problems/1901.cpp) | | 1903 | Easy | [Largest Odd Number in String](Problems/1903.cpp) | | 1905 | Medium | [Count Sub Islands](Problems/1905.cpp) | | 1907 | Medium | [Count Salary Categories](Problems/1907.cpp) |