Skip to content

Commit

Permalink
Find Minimum in Rotated Sorted Array II
Browse files Browse the repository at this point in the history
  • Loading branch information
haoel committed Oct 21, 2014
1 parent 4cda310 commit f3428f9
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LeetCode C++ Solutions

| Title | Solution | Add Date | Difficulty |
| ----- | -------- | -------- | ---------- |
|[Find Minimum in Rotated Sorted Array II](https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/)| [C++](./src/findMinimumInRotatedSortedArray/findMinimumInRotatedSortedArray.II.cpp)|2014/10/20|Hard|
|[Find Minimum in Rotated Sorted Array](https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/)| [C++](./src/findMinimumInRotatedSortedArray/findMinimumInRotatedSortedArray.cpp)|2014/10/15|Medium|
|[Maximum Product Subarray](https://oj.leetcode.com/problems/maximum-product-subarray/)| [C++](./src/maximumProductSubarray/maximumProductSubarray.cpp)|2014/9/23|Medium|
|[Reverse Words in a String](https://oj.leetcode.com/problems/reverse-words-in-a-string/)| [C++](./src/reverseWordsInAString/reverseWordsInAString.cpp)|2014/3/5|Medium|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// Source : https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/
// Author : Hao Chen
// Date : 2014-10-21

/**********************************************************************************
*
* Follow up for "Find Minimum in Rotated Sorted Array":
* What if duplicates are allowed?
*
* Would this affect the run-time complexity? How and why?
*
* Suppose a sorted array is rotated at some pivot unknown to you beforehand.
*
* (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
*
* Find the minimum element.
*
* The array may contain duplicates.
*
**********************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


/*
* Need be very careful for the following cases:
*
* [3, 3, 3, 3, 3]
*
* [3, 3, 3, 1, 3]
*
*/

int findMin(vector<int> &num) {

int low=0, high=num.size()-1;

while(high-low>1){
//skip the same element, this would cause the O(n) run-time complexity.
while (high - low > 1 && num[low] == num[high]){
low++;
}
//binary search
int mid = low + (high-low)/2;
//Notes: checking the equal situation
if (num[low] <= num[mid] && num[mid] <= num[high]){
return num[low] < num[mid] ? num[low] : num[mid];
}
//move the high pointer to the middle, if sub-array from low to mid is rotated.
if (num[low] > num [mid]){
high = mid;
continue;
}
// move the low pointer to the middle, if sub-array from mid to high is rotated.
if (num[mid] > num[high]){
low = mid;
continue;
}
}

if (high == low) return num[low];
return num[low] < num[high] ? num[low] : num[high];

}


void rotate_array(int a[], int n, int pos){
int i, from=0;
pos = pos % n;
if (n<=0) return;

int tmp = a[0];

for(int i=0, step=0; step<n && i<pos; step++){
int to;
if (from-pos < 0) {
to = n-pos+from;
}else{
to = from-pos;
}
int t ;
t = a[to];
a[to] = tmp;
tmp = t;
from = to;
if ( to == i ){
i++;
from++;
tmp = a[from];
}
}

}

void printArray(int A[], int n) {
printf("{");
for(int i=0; i<n; i++) {
printf("%d, ", A[i]);
}
printf("}\n");
}

int main(int argc, char** argv)
{

int cnt=20;

if (argc>1) {
cnt = atoi(argv[1]);
}

srand(time(NULL));

int expectedMin, actualMin;
int *a = new int[cnt];
for(int n=0; n<=cnt; n++) {
printf("--------------------------------------\n");
//generate the array with random elements
for(int i=0; i<cnt; i++){
a[i]=rand()%cnt;
}
//sort the array
sort(a, a+cnt);
expectedMin = a[0];
//printArray(a, cnt);
int rotate_pos = random() % cnt;
//rotate_pos=2;
printf("rotate=%d\n", rotate_pos);
rotate_array(a, cnt, rotate_pos);
printArray(a, cnt);

vector<int> num(a, a+cnt);
actualMin = findMin(num);
cout << "findMin = " << actualMin << " " << (expectedMin==actualMin ? "passed" : "failed") << endl;

}
delete[] a;

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,14 @@ int main(int argc, char** argv)

srand(time(NULL));

int expectedMin, actualMin;
int *a = new int[cnt];
for(int n=0; n<=cnt; n++) {
printf("--------------------------------------\n");
for(int i=0; i<cnt; i++){
a[i]=i;
}
expectedMin = a[0];
//printArray(a, cnt);
int rotate_pos = random() % cnt;
//rotate_pos=2;
Expand All @@ -107,7 +109,8 @@ int main(int argc, char** argv)
printArray(a, cnt);

vector<int> num(a, a+cnt);
cout << "findMin = " << findMin(num) << endl;
actualMin = findMin(num);
cout << "findMin = " << actualMin << " " << (expectedMin==actualMin ? "passed" : "failed") << endl;

}
delete[] a;
Expand Down

0 comments on commit f3428f9

Please sign in to comment.