leetcode

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

commit 4faa838ae100c53005b7e71f98cd7157e2d458b0
parent 32a99662b8545cfd76633955ff0758692a8575a4
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Sun, 24 Nov 2024 18:48:29 +0100

1 Random Problem

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

diff --git a/Problems/0665.cpp b/Problems/0665.cpp @@ -0,0 +1,25 @@ +class Solution { + public: + bool checkPossibility(const vector<int> &nums) const { + const int n = size(nums); + int cnt1 = 0, cnt2 = 0; + + for (int i = 1, prev = nums[0]; i < n && cnt1 <= 1; i++) { + if (nums[i] < prev) + cnt1++; + else + prev = nums[i]; + } + + if (cnt1 <= 1) return true; + + for (int i = n - 2, prev = nums[n - 1]; i >= 0 && cnt2 <= 1; i--) { + if (nums[i] > prev) + cnt2++; + else + prev = nums[i]; + } + + return cnt2 <= 1; + } +}; diff --git a/README.md b/README.md @@ -519,6 +519,7 @@ reference and a base for solving problems. | 0661 | Easy | [Image Smoother](Problems/0661.cpp) | | 0662 | Medium | [Maximum Width of Binary Tree](Problems/0662.cpp) | | 0664 | Hard | [Strange Printer](Problems/0664.cpp) | +| 0665 | Medium | [Non-decreasing Array](Problems/0665.cpp) | | 0667 | Medium | [Beautiful Arrangement II](Problems/0667.cpp) | | 0669 | Medium | [Trim a Binary Search Tree](Problems/0669.cpp) | | 0670 | Medium | [Maximum Swap](Problems/0670.cpp) |