Skip to content

Commit 407832e

Browse files
committed
Merge pull request #6 from FreeTymeKiyan/xinyu-liu
C++/162_Find_Peak_Element.cpp
2 parents edf0ca9 + b3d5f12 commit 407832e

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Problem 81, Search in Rotated Sorted Array II
2+
3+
// Follow up for "Search in Rotated Sorted Array":
4+
// What if duplicates are allowed?
5+
// Would this affect the run-time complexity? How and why?
6+
// Write a function to determine if a given target is in the array.
7+
//
8+
// FYI: "Search in Rotated Sorted Array":
9+
// Suppose a sorted array is rotated at some pivot unknown to you beforehand.
10+
// (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
11+
// You are given a target value to search. If found in the array return its index, otherwise return -1.
12+
// You may assume no duplicate exists in the array.
13+
14+
#include <stddef.h>
15+
#include <vector>
16+
#include <string.h>
17+
#include <stdio.h>
18+
#include <algorithm>
19+
#include <iostream>
20+
#include <map>
21+
22+
using namespace std;
23+
24+
class Solution {
25+
public:
26+
bool search(vector<int>& nums, int target) {
27+
28+
int start = 0;
29+
int end = nums.size()-1;
30+
31+
while(start <= end){
32+
// skip the repeating numbers on both sides
33+
while(start < end && nums[start] == nums[start + 1]) start++;
34+
while(start < end && nums[end] == nums[end - 1]) end--;
35+
36+
int mid = (start + end)/2;
37+
if(nums[mid] == target) return true; // found
38+
if(nums[mid] < target){
39+
// for cases like {6 7 0 1 2 4 5} mid is 1, start is 6, end is 5, target is 2
40+
// and cases like {2 4 5 6 7 0 1} mid is 6, start is 2, end is 1, target is 7
41+
if(nums[start] > target || nums[mid] >= nums[start])
42+
start = mid + 1;
43+
else
44+
end = mid - 1;
45+
}
46+
else{
47+
// for cases like {4 5 6 7 0 1 2}, mid is 7, start is 4, end is 2, target is 5
48+
// and cases like {6 7 0 1 2 4 5}, mid is 1, start is 6, end is 5, target is 0
49+
if(nums[start] <= target || nums[mid] < nums[start])
50+
end = mid - 1;
51+
else
52+
start = mid + 1;
53+
}
54+
}
55+
return false;
56+
}
57+
};
58+
int main()
59+
{
60+
Solution* sol = new Solution();
61+
vector<int> nums;
62+
nums.push_back(4);
63+
nums.push_back(5);
64+
nums.push_back(6);
65+
nums.push_back(7);
66+
nums.push_back(0);
67+
nums.push_back(1);
68+
nums.push_back(2);
69+
70+
cout<<sol->search(nums,3)<<endl;
71+
cout<<sol->search(nums,0)<<endl;
72+
cout<<sol->search(nums,6)<<endl;
73+
cout<<sol->search(nums,5)<<endl;
74+
75+
char c;
76+
cin>>c;
77+
78+
return 0;
79+
}

C/162_Find_Peak_Element.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
A peak element is an element that is greater than its neighbors.
3+
Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.
4+
The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
5+
You may imagine that num[-1] = num[n] = -∞.
6+
7+
Example:
8+
in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.
9+
10+
Tag: Array, Binary Search
11+
12+
Author: Xinyu Liu
13+
*/
14+
15+
#include <iostream>
16+
#include <vector>
17+
using namespace std;
18+
19+
class Solution {
20+
21+
public:
22+
int findPeakElement(vector<int>& nums) {
23+
int sz = nums.size();
24+
if (sz == 1)
25+
return 0;
26+
if (nums.at(0) > nums.at(1))
27+
return 0;
28+
if (nums.at(sz-1) > nums.at(sz-2))
29+
return sz-1;
30+
31+
for(int i = 1; i < nums.size(); i++)
32+
{
33+
if (nums.at(i-1) < nums.at(i) & nums.at(i) > nums.at(i+1))
34+
return i;
35+
}
36+
return -1;
37+
}
38+
};
39+
40+
void main(int argc, char ** argv){
41+
42+
// Initialize vector
43+
int myints[] = {1,2,3,4,3};
44+
int index;
45+
std::vector<int> test (myints, myints + sizeof(myints) / sizeof(int) );
46+
47+
Solution sol;
48+
index = sol.findPeakElement (test);
49+
50+
printf("%i \n",index);
51+
52+
}

0 commit comments

Comments
 (0)