|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -import com.fishercoder.common.classes.Interval; |
4 | 3 | import com.google.gson.JsonArray;
|
5 | 4 | import com.google.gson.JsonElement;
|
6 | 5 | import com.google.gson.JsonObject;
|
@@ -238,160 +237,6 @@ public String findLongestRepeatedSubstring(String s) {
|
238 | 237 | }
|
239 | 238 | }
|
240 | 239 |
|
241 |
| - |
242 |
| - public static class RangeModule { |
243 |
| - /** |
244 |
| - * OA on 9/30/2017 |
245 |
| - */ |
246 |
| - List<Interval> intervals; |
247 |
| - |
248 |
| - public RangeModule() { |
249 |
| - this.intervals = new ArrayList<>(); |
250 |
| - } |
251 |
| - |
252 |
| - public void addRange(int lower, int upper) { |
253 |
| - intervals = addRange(intervals, new Interval(lower, upper)); |
254 |
| - |
255 |
| - } |
256 |
| - |
257 |
| - private List<Interval> addRange(List<Interval> intervals, Interval newInterval) { |
258 |
| - List<Interval> result = new ArrayList<>(); |
259 |
| - int i = 0; |
260 |
| - // add all the intervals ending before newInterval starts |
261 |
| - while (i < intervals.size() && intervals.get(i).end < newInterval.start) { |
262 |
| - result.add(intervals.get(i++)); |
263 |
| - } |
264 |
| - // merge all overlapping intervals to one considering newInterval |
265 |
| - while (i < intervals.size() && intervals.get(i).start <= newInterval.end) { |
266 |
| - newInterval = new Interval( // we could mutate newInterval here also |
267 |
| - Math.min(newInterval.start, intervals.get(i).start), |
268 |
| - Math.max(newInterval.end, intervals.get(i).end)); |
269 |
| - i++; |
270 |
| - } |
271 |
| - result.add(newInterval); |
272 |
| - // add all the rest |
273 |
| - while (i < intervals.size()) { |
274 |
| - result.add(intervals.get(i++)); |
275 |
| - } |
276 |
| - return result; |
277 |
| - } |
278 |
| - |
279 |
| - public boolean queryRange(int lower, int upper) { |
280 |
| - /**check two ends first*/ |
281 |
| - if (intervals.get(0).start > upper || intervals.get(intervals.size() - 1).end < lower) { |
282 |
| - return false; |
283 |
| - } |
284 |
| - |
285 |
| - /**Since intervals are sorted, I can use binary search for this query range to achieve log(n) (best) time complexity*/ |
286 |
| - |
287 |
| - int left = 0; |
288 |
| - int right = intervals.size() - 1; |
289 |
| - int start;//this is the index of the interval that has overlapping with "lower" |
290 |
| - while (left < right) { |
291 |
| - int mid = left + (right - left) / 2; |
292 |
| - int pos = isInRange(intervals.get(mid), lower); |
293 |
| - if (pos == 0) { |
294 |
| - start = mid; |
295 |
| - if (intervals.get(start).end >= upper) { |
296 |
| - return true; |
297 |
| - } else { |
298 |
| - return false; |
299 |
| - } |
300 |
| - } else if (pos < 0) { |
301 |
| - right = mid - 1; |
302 |
| - } else { |
303 |
| - left = mid + 1; |
304 |
| - } |
305 |
| - } |
306 |
| - int pos = isInRange(intervals.get(left), lower); |
307 |
| - if (pos == 0) { |
308 |
| - if (intervals.get(left).end >= upper) { |
309 |
| - return true; |
310 |
| - } else { |
311 |
| - return false; |
312 |
| - } |
313 |
| - } else { |
314 |
| - return false; |
315 |
| - } |
316 |
| - } |
317 |
| - |
318 |
| - private int isInRange(Interval interval, int lower) { |
319 |
| - if (interval.start <= lower && lower <= interval.end) { |
320 |
| - return 0; |
321 |
| - } else if (interval.start > lower) { |
322 |
| - return -1;//this means lower is on the left part of this interval |
323 |
| - } else { |
324 |
| - return 1;//this means lower is on the right part of this interval |
325 |
| - } |
326 |
| - } |
327 |
| - |
328 |
| - public void deleteRange(int lower, int upper) { |
329 |
| - /**check two ends first*/ |
330 |
| - if (intervals.get(0).start > upper || intervals.get(intervals.size() - 1).end < lower) { |
331 |
| - return; |
332 |
| - } |
333 |
| - |
334 |
| - /**Since intervals are sorted, one can use binary search for this query range to achieve log(n) (best) time complexity*/ |
335 |
| - int left = 0; |
336 |
| - int right = intervals.size() - 1; |
337 |
| - int start = Integer.MIN_VALUE;//this is the index of the interval that has overlapping with "lower" |
338 |
| - while (left < right) { |
339 |
| - int mid = left + (right - left) / 2; |
340 |
| - int pos = isInRange(intervals.get(mid), lower); |
341 |
| - if (pos == 0) { |
342 |
| - start = mid; |
343 |
| - break; |
344 |
| - } else if (pos < 0) { |
345 |
| - right = mid - 1; |
346 |
| - } else { |
347 |
| - left = mid + 1; |
348 |
| - } |
349 |
| - } |
350 |
| - if (start == Integer.MIN_VALUE) { |
351 |
| - start = left; |
352 |
| - } |
353 |
| - Interval startInterval = intervals.get(start); |
354 |
| - intervals.remove(start);//remove this interval first |
355 |
| - |
356 |
| - if (startInterval.start < lower - 1) { |
357 |
| - addRange(startInterval.start, lower - 1); |
358 |
| - } |
359 |
| - |
360 |
| - if (startInterval.end > upper + 1) { |
361 |
| - addRange(upper + 1, startInterval.end); |
362 |
| - } |
363 |
| - |
364 |
| - if (startInterval.end < upper) { |
365 |
| - //only in this case, we'll have to do the following, otherwise we don't need to do anything but just return |
366 |
| - |
367 |
| - int end = start;//find the index of the interval that overlapping with upper |
368 |
| - left = start + 1; |
369 |
| - right = intervals.size() - 1; |
370 |
| - while (left < right) { |
371 |
| - int mid = left + (right - left) / 2; |
372 |
| - int pos = isInRange(intervals.get(mid), upper); |
373 |
| - if (pos == 0) { |
374 |
| - end = mid; |
375 |
| - break; |
376 |
| - } else if (pos < 0) { |
377 |
| - right = mid - 1; |
378 |
| - } else { |
379 |
| - left = mid + 1; |
380 |
| - } |
381 |
| - } |
382 |
| - Interval endInterval = intervals.get(end);//retrieve this interval first before removing the others |
383 |
| - |
384 |
| - //remove all of the ranges up to end |
385 |
| - for (int i = start + 1; i <= end; i++) { |
386 |
| - intervals.remove(i); |
387 |
| - } |
388 |
| - |
389 |
| - addRange(upper + 1, endInterval.end); |
390 |
| - } |
391 |
| - |
392 |
| - } |
393 |
| - } |
394 |
| - |
395 | 240 | public static String getShiftedString(String s, int left, int right) {
|
396 | 241 | if (left == right) {
|
397 | 242 | return s;
|
|
0 commit comments