-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearch.java
229 lines (208 loc) · 6.49 KB
/
BinarySearch.java
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
public class BinarySearch {
public static void main(String[] args) {
BinarySearch b = new BinarySearch();
int[] array = new int[] { 3, 4, 5, 6, 6, 12, 16 };
System.out.println(b.closest(array, 10));
}
/**
* classical binarySearch
*/
public int binarySearch(int[] array, int target) {
if (array == null || array.length == 0) {
return -1;
}
int left = 0;
int right = array.length - 1;
int mid;
while (left <= right) {
mid = left + (right - left) / 2;
if (array[mid] == target) {
return mid;
}
if (array[mid] > target) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return -1;
}
/**
* Search in 2D
* <p>
* Given a 2D matrix that contains integers only, which each row is sorted in an
* ascending order. The first element of next row is larger than (or equal to)
* the last element of previous row.
* Given a target number, returning the position that the target locates within
* the matrix. If the target number does not exist in the matrix, return {-1,
* -1}.
*/
public int[] search(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return new int[] { -1, -1 };
}
int H = matrix.length;
int W = matrix[0].length;
int left = 0;
int right = H * W - 1;
int mid;
while (left <= right) {
mid = left + (right - left) / 2;
int y = mid % W;
int x = mid / W;
if (matrix[x][y] == target) {
return new int[] { x, y };
}
if (matrix[x][y] > target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return new int[] { -1, -1 };
}
/**
* Closest
* <p>
* Given a target integer T and an integer array A sorted in ascending order,
* find the index i in A such that A[i] is closest to T.
*/
public int closest(int[] array, int target) {
// Write your solution here
if (array == null || array.length == 0) {
return -1;
}
int left = 0;
int right = array.length - 1;
int mid;
while (left + 1 < right) {
mid = left + (right - left) / 2;
if (array[mid] > target) {
right = mid;
} else {
left = mid;
}
}
return Math.abs(array[left] - target) > Math.abs(array[right] - target) ? right : left;
}
/**
* First Occur
* <p>
* Given a target integer T and an integer array A sorted in ascending order,
* find the index of the first occurrence of T in A or return -1 if there is no
* such index.
*/
public int firstOccur(int[] array, int target) {
if (array == null || array.length == 0) {
return -1;
}
int left = 0;
int right = array.length - 1;
int mid;
while (left < right) {
mid = left + (right - left) / 2;
if (array[mid] > target) {
right = mid;
} else {
left = mid + 1;
}
}
return array[left] == target ? left : -1;
}
/**
* K Cloest
* <p>
* Given a target integer T, a non-negative integer K and an integer array A
* sorted in ascending order, find the K closest numbers to T in A. If there is
* a tie, the smaller elements are always preferred.
*/
public int[] kClosest(int[] array, int target, int k) {
if (array == null || array.length == 0) {
return array;
}
if (k == 0) {
return new int[0];
}
int closest = closest(array, target);
int left = closest;
int right = closest;
int count = 0;
int[] result = new int[k];
while (count < k) {
if (right == array.length) {
result[count++] = array[left--];
} else if (left == -1) {
result[count++] = array[right++];
} else if (Math.abs(array[left] - target) <= Math.abs(array[right] - target)) {
result[count++] = array[left--];
} else {
result[count++] = array[right++];
}
}
return result;
}
/**
* Smallest Element Larger than Target
* <p>
* Given a target integer T and an integer array A sorted in ascending order,
* find the index of the smallest element in A that is larger than T or return
* -1 if there is no such index.
*/
public int smallestElementLargerThanTarget(int[] array, int target) {
if (array == null || array.length == 0 || array[array.length - 1] <= target) {
return -1;
}
if (array[0] > target) {
return 0;
}
int left = 0;
int right = array.length - 1;
int mid;
while (left < right - 1) {
mid = left + (right - left) / 2;
if (array[mid] > target) {
right = mid;
} else {
left = mid;
}
}
return right;
}
/**
* Search In Unknown Sized Sorted Array
* <p>
* Given a integer dictionary A of unknown size, where the numbers in the
* dictionary are sorted in ascending order, determine if a given target integer
* T is in the dictionary. Return the index of T in A, return -1 if T is not in
* A.
*/
public int search(Dictionary dict, int target) {
int size = 1;
while (dict.get(size) != null && dict.get(size) < target) {
size <<= 1;
}
int left = size / 2;
int right = size;
while (left <= right) {
int mid = left + (right - left) / 2;
if (dict.get(mid) == null || dict.get(mid) > target) {
right = mid - 1;
} else if (dict.get(mid) == target) {
return mid;
} else {
left = mid + 1;
}
}
return -1;
}
private class Dictionary {
private int[] array;
public Integer get(int index) {
if (index >= array.length) {
return null;
} else {
return array[index];
}
}
}
}