-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path702.search-in-unknown-array.cpp
More file actions
183 lines (168 loc) · 4.06 KB
/
702.search-in-unknown-array.cpp
File metadata and controls
183 lines (168 loc) · 4.06 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <iostream>
#include <vector>
using namespace std;
// Forward declaration of ArrayReader class.
/**
*
*
* Solution 1
* Discussion:
* This solution is based on the fact that we don't know array length
* Otherwise this could be easily be solved with Binary Search in O(logN)
*
* Algorigthm :
* Use step variable to quickly skip over array to find boundries
* Multiply step by 2 every time boundry not found
* Or if step goes out of boundry reset back to 0
*
* where Start value >= target and End Value is <= target
* When those boundries are found use BinarySearch to find target
*
* if boundries not found return -1
*
* same if binary search could not find target
*
* Runtime: 56 ms, faster than 41.36% of C++ online submissions for Search in a Sorted Array of Unknown Size.
* Memory Usage: 12.3 MB, less than 33.33% of C++ online submissions for Search in a Sorted Array of
*
* */
class Solution
{
public:
int search(const ArrayReader &reader, int target)
{
int stIdx = 0, vol = 1;
int found = -1;
while (found == -1 || reader.get(stIdx) != 2147483647)
{
if (reader.get(stIdx + vol) == 2147483647)
{
vol /= 2;
}
if (reader.get(stIdx) < target && reader.get(stIdx + vol) < target && reader.get(stIdx + vol) != 2147483647)
{
stIdx += vol;
vol *= 2;
}
else if (reader.get(stIdx) <= target && reader.get(stIdx + vol) >= target)
{
//do binary search
found = binarySearch(stIdx, stIdx + vol, reader, target);
break;
}
else
{
break;
}
}
return found;
}
int binarySearch(int start, int end, const ArrayReader &reader, int target)
{
int mid = 0;
int found = -1;
while (start <= end && found == -1)
{
mid = (start + end) / 2;
if (reader.get(mid) == target)
{
return found = mid;
}
else if (reader.get(mid) > target)
{
end = mid - 1;
}
else
{
start = mid + 1;
}
}
return found;
}
};
/**
*
* Solution 2
*
* Discussion:
* Try use binary search all the way. Setting End pointer to max integer value
* Seems more elegant and twice faster than Soluition 1
*
* Memory wise lacks behind.
*
*
*
* Runtime: 52 ms, faster than 84.69% of C++ online submissions for Search in a Sorted Array of Unknown Size.
* Memory Usage: 12.3 MB, less than 11.90% of C++ online submissions for Search in a Sorted Array of Unknown Size.
*
* Complexity O(LogN)
* Space O(1)
* */
class Solution2
{
public:
int search(const ArrayReader &reader, int target)
{
int l = 0, r = 2147483647, mid;
int found = -1;
while (found == -1 && l <= r)
{
mid = (l + r) / 2;
if (reader.get(mid) == target)
{
return found = mid;
}
if (reader.get(mid) == 2147483647 || reader.get(mid) > target)
{
r = mid - 1;
}
if (reader.get(mid) < target)
{
l = mid + 1;
}
}
return found;
}
};
/**
*
*
* Solution 3
* Recursive version of Solution 2
*
* Recursive solution is a little faster.
* However takes a bit more memory than Solution 2
*
* Runtime: 48 ms, faster than 87.41% of C++ online submissions for Search in a Sorted Array of Unknown Size.
* Memory Usage: 12.5 MB, less than 7.14% of C++ online submissions for Search in a Sorted Array of Unknown Size.
*
* Complexity O(LogN)
* Space O(1)
* */
class Solution3
{
public:
int search(const ArrayReader &reader, int target)
{
int l = 0, r = 2147483647, mid;
return binarySearch(l, r, reader, target);
}
int binarySearch(int l, int r, const ArrayReader &reader, int target)
{
if (r >= 1)
{
int mid = (l + r) / 2;
if (reader.get(mid) == target)
return mid;
if (reader.get(mid) == 2147483647 || reader.get(mid) > target)
return binarySearch(l, mid - 1, reader, target);
if (reader.get(mid) < target)
return binarySearch(mid + 1, r, reader, target);
}
return -1;
}
};
int main()
{
return 0;
}