Skip to content

Commit f3428f9

Browse files
committed
Find Minimum in Rotated Sorted Array II
1 parent 4cda310 commit f3428f9

File tree

3 files changed

+151
-1
lines changed

3 files changed

+151
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LeetCode C++ Solutions
55

66
| Title | Solution | Add Date | Difficulty |
77
| ----- | -------- | -------- | ---------- |
8+
|[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|
89
|[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|
910
|[Maximum Product Subarray](https://oj.leetcode.com/problems/maximum-product-subarray/)| [C++](./src/maximumProductSubarray/maximumProductSubarray.cpp)|2014/9/23|Medium|
1011
|[Reverse Words in a String](https://oj.leetcode.com/problems/reverse-words-in-a-string/)| [C++](./src/reverseWordsInAString/reverseWordsInAString.cpp)|2014/3/5|Medium|
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// Source : https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/
2+
// Author : Hao Chen
3+
// Date : 2014-10-21
4+
5+
/**********************************************************************************
6+
*
7+
* Follow up for "Find Minimum in Rotated Sorted Array":
8+
* What if duplicates are allowed?
9+
*
10+
* Would this affect the run-time complexity? How and why?
11+
*
12+
* Suppose a sorted array is rotated at some pivot unknown to you beforehand.
13+
*
14+
* (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
15+
*
16+
* Find the minimum element.
17+
*
18+
* The array may contain duplicates.
19+
*
20+
**********************************************************************************/
21+
22+
#include <stdio.h>
23+
#include <stdlib.h>
24+
#include <time.h>
25+
#include <iostream>
26+
#include <vector>
27+
#include <algorithm>
28+
using namespace std;
29+
30+
31+
/*
32+
* Need be very careful for the following cases:
33+
*
34+
* [3, 3, 3, 3, 3]
35+
*
36+
* [3, 3, 3, 1, 3]
37+
*
38+
*/
39+
40+
int findMin(vector<int> &num) {
41+
42+
int low=0, high=num.size()-1;
43+
44+
while(high-low>1){
45+
//skip the same element, this would cause the O(n) run-time complexity.
46+
while (high - low > 1 && num[low] == num[high]){
47+
low++;
48+
}
49+
//binary search
50+
int mid = low + (high-low)/2;
51+
//Notes: checking the equal situation
52+
if (num[low] <= num[mid] && num[mid] <= num[high]){
53+
return num[low] < num[mid] ? num[low] : num[mid];
54+
}
55+
//move the high pointer to the middle, if sub-array from low to mid is rotated.
56+
if (num[low] > num [mid]){
57+
high = mid;
58+
continue;
59+
}
60+
// move the low pointer to the middle, if sub-array from mid to high is rotated.
61+
if (num[mid] > num[high]){
62+
low = mid;
63+
continue;
64+
}
65+
}
66+
67+
if (high == low) return num[low];
68+
return num[low] < num[high] ? num[low] : num[high];
69+
70+
}
71+
72+
73+
void rotate_array(int a[], int n, int pos){
74+
int i, from=0;
75+
pos = pos % n;
76+
if (n<=0) return;
77+
78+
int tmp = a[0];
79+
80+
for(int i=0, step=0; step<n && i<pos; step++){
81+
int to;
82+
if (from-pos < 0) {
83+
to = n-pos+from;
84+
}else{
85+
to = from-pos;
86+
}
87+
int t ;
88+
t = a[to];
89+
a[to] = tmp;
90+
tmp = t;
91+
from = to;
92+
if ( to == i ){
93+
i++;
94+
from++;
95+
tmp = a[from];
96+
}
97+
}
98+
99+
}
100+
101+
void printArray(int A[], int n) {
102+
printf("{");
103+
for(int i=0; i<n; i++) {
104+
printf("%d, ", A[i]);
105+
}
106+
printf("}\n");
107+
}
108+
109+
int main(int argc, char** argv)
110+
{
111+
112+
int cnt=20;
113+
114+
if (argc>1) {
115+
cnt = atoi(argv[1]);
116+
}
117+
118+
srand(time(NULL));
119+
120+
int expectedMin, actualMin;
121+
int *a = new int[cnt];
122+
for(int n=0; n<=cnt; n++) {
123+
printf("--------------------------------------\n");
124+
//generate the array with random elements
125+
for(int i=0; i<cnt; i++){
126+
a[i]=rand()%cnt;
127+
}
128+
//sort the array
129+
sort(a, a+cnt);
130+
expectedMin = a[0];
131+
//printArray(a, cnt);
132+
int rotate_pos = random() % cnt;
133+
//rotate_pos=2;
134+
printf("rotate=%d\n", rotate_pos);
135+
rotate_array(a, cnt, rotate_pos);
136+
printArray(a, cnt);
137+
138+
vector<int> num(a, a+cnt);
139+
actualMin = findMin(num);
140+
cout << "findMin = " << actualMin << " " << (expectedMin==actualMin ? "passed" : "failed") << endl;
141+
142+
}
143+
delete[] a;
144+
145+
return 0;
146+
}

src/findMinimumInRotatedSortedArray/findMinimumInRotatedSortedArray.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,14 @@ int main(int argc, char** argv)
9393

9494
srand(time(NULL));
9595

96+
int expectedMin, actualMin;
9697
int *a = new int[cnt];
9798
for(int n=0; n<=cnt; n++) {
9899
printf("--------------------------------------\n");
99100
for(int i=0; i<cnt; i++){
100101
a[i]=i;
101102
}
103+
expectedMin = a[0];
102104
//printArray(a, cnt);
103105
int rotate_pos = random() % cnt;
104106
//rotate_pos=2;
@@ -107,7 +109,8 @@ int main(int argc, char** argv)
107109
printArray(a, cnt);
108110

109111
vector<int> num(a, a+cnt);
110-
cout << "findMin = " << findMin(num) << endl;
112+
actualMin = findMin(num);
113+
cout << "findMin = " << actualMin << " " << (expectedMin==actualMin ? "passed" : "failed") << endl;
111114

112115
}
113116
delete[] a;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy