-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path34.find-first-and-last-position-of-element-in-sorted-array.cpp
92 lines (73 loc) · 2.64 KB
/
34.find-first-and-last-position-of-element-in-sorted-array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* @lc app=leetcode id=34 lang=cpp
*
* [34] Find First and Last Position of Element in Sorted Array
*/
// @lc code=start
#include <algorithm>
#include <iostream>
#include <memory.h>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
// #define DEBUG
class Solution {
public:
int binarySearch(vector<int>& nums, int startingIndex, vector<int>& result, int target) {
int medianIndex = nums.size() / 2;
int median = nums[medianIndex];
int offset = nums.size() - medianIndex;
if (median > target) {
if (nums.size() == 1) {
return -1;
}
#ifdef DEBUG
cout << "Starting Index: " << startingIndex << "\n";
for (int i = 0; i < nums.size(); i++) {
cout << i << " th nums: " << nums[i];
}
#endif
vector<int> leftArr = vector<int>(nums.begin(), nums.end() - offset);
return binarySearch(leftArr, startingIndex, result, target);
}
else if (median < target) {
if (nums.size() == 1) {
return -1;
}
#ifdef DEBUG
cout << "Starting Index: " << startingIndex << "\n";
for (int i = 0; i < nums.size(); i++) {
cout << i << " th nums: " << nums[i] << "\n";
}
#endif
vector<int> rightArr = vector<int>(nums.begin() + offset, nums.end());
int newStartingPoint = nums.size() % 2 == 1 ? startingIndex + medianIndex + 1 : startingIndex + medianIndex;
return binarySearch(rightArr, newStartingPoint, result, target);
}
// median == target
else {
#ifdef DEBUG
cout << "Starting Index: " << startingIndex << "\n";
for (int i = 0; i < nums.size(); i++) {
cout << i << " th nums: " << nums[i] << "\n";
}
#endif
int maxIndex = medianIndex;
while ((maxIndex + 1 < nums.size()) && (nums[maxIndex + 1] == median)) { maxIndex++; }
result.push_back(maxIndex + startingIndex);
int minIndex = medianIndex;
while ((minIndex - 1 >= 0) && (nums[minIndex - 1] == target)) { minIndex--; }
result.insert(result.begin(), minIndex + startingIndex);
return medianIndex;
}
}
vector<int> searchRange(vector<int>& nums, int target) {
if (nums.size() < 1) return { -1, -1 };
vector<int> result = {};
if (binarySearch(nums, 0, result, target) == -1) return { -1, -1 };
return result;
}
};
// @lc code=end