mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2025-12-06 06:24:07 +00:00
day 40: 153-find-minimum-in-rotated-sorted-array
This commit is contained in:
parent
860fa91880
commit
5f1e2efa9e
10
153-find-minimum-in-rotated-sorted-array/driver.cpp
Normal file
10
153-find-minimum-in-rotated-sorted-array/driver.cpp
Normal file
@ -0,0 +1,10 @@
|
||||
#include "soln.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<int> nums = {7, -1, 0, 1, 2, 3, 4, 5, 6};
|
||||
int answer = -1;
|
||||
Solution soln;
|
||||
cout << "Found minimum correctly? " << soln.test(nums, answer) << endl;
|
||||
return 0;
|
||||
}
|
||||
31
153-find-minimum-in-rotated-sorted-array/soln.cpp
Normal file
31
153-find-minimum-in-rotated-sorted-array/soln.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
#include "soln.hpp"
|
||||
|
||||
int Solution::findMin(vector<int> &nums)
|
||||
{
|
||||
int low = 0, high = nums.size() - 1, mid;
|
||||
while (low < high)
|
||||
{
|
||||
// current subarray is unrotated
|
||||
if (nums[high] > nums[low])
|
||||
{
|
||||
return nums[low];
|
||||
}
|
||||
mid = (low + high) / 2;
|
||||
// left half
|
||||
if (nums[low] > nums[mid])
|
||||
{
|
||||
high = mid;
|
||||
}
|
||||
// right half
|
||||
else
|
||||
{
|
||||
low = mid + 1;
|
||||
}
|
||||
}
|
||||
return nums[low];
|
||||
}
|
||||
|
||||
bool Solution::test(vector<int> &nums, int answer)
|
||||
{
|
||||
return findMin(nums) == answer;
|
||||
}
|
||||
8
153-find-minimum-in-rotated-sorted-array/soln.hpp
Normal file
8
153-find-minimum-in-rotated-sorted-array/soln.hpp
Normal file
@ -0,0 +1,8 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
class Solution
|
||||
{
|
||||
public:
|
||||
int findMin(vector<int> &nums);
|
||||
bool test(vector<int> &nums, int answer);
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user