Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find minimum in rotated sorted array #190

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions leetcode-cc/FindMinInRotatedSortedArray.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <algorithm>

#include "TestHelper.h"
#include "problem.h"
#include "solution.h"

using namespace std;

IMPLEMENT_PROBLEM_CLASS(PFindMinInRotatedSortedArray, 153, DIFFI_MEDIUM,
TOPIC_ALGORITHMS,
"Find Minimum in Rotated Sorted Array",
"Given the sorted rotated array nums of unique "
"elements, return the minimum element of this array.",
{"Array"});

class SFindMinInRotatedSortedArray : public ISolution {
public:
size_t problemId() const override { return 153; }
string name() const override {
return ("Solution for " + string("Find Minimum in Rotated Sorted Array"));
}
string location() const override { return __FILE_NAME__; }
int test() const override {
return testHelper<vector<int>, int>(
{{3, 4, 5, 1, 2}, {4, 5, 6, 7, 0, 1, 2}, {11, 13, 15, 17}, {2, 1}},
{1, 0, 11, 1}, [this](auto input) { return this->findMin(input); });
};
int benchmark() const override { return 0; }

private:
int findMin(vector<int>& nums) const {
int n = nums.size();
int start = 0;
int end = n - 1;

int res = INT_MAX;

while (start <= end) {
int middle = (start + end) / 2;

if (nums[start] <= nums[middle]) {
// start..middle is sorted
res = min(res, nums[start]);
start = middle + 1;
} else {
// middle..end is sorted
res = min(res, nums[middle]);
end = middle - 1;
}
}

return res;
}
};
8 changes: 8 additions & 0 deletions runtime-cc/src/registration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "../leetcode-cc/DistinctSubsequences.hpp"
#include "../leetcode-cc/DivideTwoIntegers.hpp"
#include "../leetcode-cc/EvaluateReversePolishNotation.hpp"
#include "../leetcode-cc/FindMinInRotatedSortedArray.hpp"
#include "../leetcode-cc/FindPlayersWithZeroOrOneLosses.hpp"
#include "../leetcode-cc/FlattenBTreeToLinkedList.hpp"
#include "../leetcode-cc/FourSum.hpp"
Expand Down Expand Up @@ -665,5 +666,12 @@ const int registerAll(std::shared_ptr<Container> handle) {
handle->registerSolution(
[]() -> ArcSolution { return std::make_shared<SMaxProductSubarray>(); });

handle->registerProblem([]() -> ArcProblem {
return std::make_shared<PFindMinInRotatedSortedArray>();
});
handle->registerSolution([]() -> ArcSolution {
return std::make_shared<SFindMinInRotatedSortedArray>();
});

return 0;
}
Loading