leetcode

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

commit af407a9527e955088a16bd38ae884a5a843ed5a4
parent ac5b68626e56edd5f0c2bcc4c40cab49d15bb983
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed, 12 Jun 2024 11:53:11 +0200

1 Random Problem

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

diff --git a/Problems/3011.cpp b/Problems/3011.cpp @@ -0,0 +1,24 @@ +class Solution { + public: + bool canSortArray(vector<int> &nums) { + const int n = size(nums); + + int i = 0, prev = 0, cnt = std::popcount((unsigned)nums[0]); + int mini = nums[0], maxi = nums[0]; + for (int j = 1; j < n; j++) { + const int crnt = std::popcount((unsigned)nums[j]); + if (cnt == crnt) { + mini = min(mini, nums[j]); + maxi = max(maxi, nums[j]); + continue; + } + if (mini < prev) return false; + prev = maxi; + mini = maxi = nums[j]; + cnt = crnt; + i = j; + } + + return mini >= prev; + } +}; diff --git a/README.md b/README.md @@ -1242,6 +1242,7 @@ for solving problems. | 2971 | Medium | [Find Polygon With the Largest Perimeter](Problems/2971.cpp) | | 2997 | Medium | [Minimum Number of Operations to Make Array XOR Equal to K](Problems/2997.cpp) | | 3005 | Easy | [Count Elements With Maximum Frequency](Problems/3005.cpp) | +| 3011 | Medium | [Find if Array Can Be Sorted](Problems/3011.cpp) | | 3015 | Medium | [Count the Number of Houses at a Certain Distance I](Problems/3015.cpp) | | 3016 | Medium | [Minimum Number of Pushes to Type Word II](Problems/3016.cpp) | | 3034 | Medium | [Number of Subarrays That Match a Pattern I](Problems/3034.cpp) |