-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindFirstAndLastPositionOfElementInSortedArray.h
128 lines (117 loc) · 3.78 KB
/
FindFirstAndLastPositionOfElementInSortedArray.h
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// FindFirstAndLastPositionOfElementInSortedArray.h
//
//
// https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/description/
//
#ifndef FindFirstAndLastPositionOfElementInSortedArray_h
#define FindFirstAndLastPositionOfElementInSortedArray_h
class Solution {
public:
void searchRangeRec(vector<int>& result, vector<int>& nums, int startIndex, int endIndex, int target)
{
if (endIndex - startIndex <= 0)
{
return;
}
int iterator;
if (endIndex - startIndex == 1)
{
iterator = startIndex + ((endIndex+1 - startIndex) / 2);
}
else
{
iterator = startIndex + ((endIndex - startIndex) / 2);
}
cout << startIndex << " " << iterator << " " << endIndex << endl;
if (nums[iterator] == target)
{
if (iterator < result[0])
{
result[0] = iterator;
}
if (iterator > result[1])
{
result[1] = iterator;
}
if (endIndex - startIndex == 1)
{
if (nums[iterator-1] == target)
{
if (iterator - 1 < result[0])
{
result[0] = iterator - 1;
}
if (iterator - 1 > result[1])
{
result[1] = iterator - 1;
}
}
return;
}
searchRangeRec(result, nums, startIndex, startIndex + ((endIndex - startIndex) / 2), target);
searchRangeRec(result, nums, startIndex + ((endIndex - startIndex) / 2), endIndex, target);
}
else if (nums[iterator] > target)
{
if (endIndex - startIndex == 1)
{
if (nums[iterator-1] == target)
{
if (iterator - 1 < result[0])
{
result[0] = iterator - 1;
}
if (iterator - 1 > result[1])
{
result[1] = iterator - 1;
}
}
return;
}
searchRangeRec(result, nums, startIndex, startIndex + ((endIndex - startIndex) / 2), target);
}
else if (nums[iterator] < target)
{
if (endIndex - startIndex == 1)
{
if (nums[iterator-1] == target)
{
if (iterator - 1 < result[0])
{
result[0] = iterator - 1;
}
if (iterator - 1 > result[1])
{
result[1] = iterator - 1;
}
}
return;
}
searchRangeRec(result, nums, startIndex + ((endIndex - startIndex) / 2), endIndex, target);
}
}
vector<int> searchRange(vector<int>& nums, int target) {
int startingIndex = INT_MAX;
int endIndex = INT_MIN;
vector<int> result;
result.push_back(startingIndex);
result.push_back(endIndex);
int limitStart = 0;
int limitEnd = nums.size() - 1;
int iterator = (limitEnd - limitStart) / 2;
searchRangeRec(result, nums, 0, nums.size() - 1, target);
if (result[0] == INT_MAX)
{
result[0] = -1;
result[1] = -1;
}
if (nums.size() == 1 && nums[0] == target)
{
result[0] = 0;
result[1] = 0;
}
return result;
}
};
#endif /* FindFirstAndLastPositionOfElementInSortedArray_h */