|
| 1 | +package stevesun.algorithms; |
| 2 | + |
| 3 | +/** |
| 4 | + * Follow up for "Search in Rotated Sorted Array": |
| 5 | + What if duplicates are allowed? |
| 6 | +
|
| 7 | + Would this affect the run-time complexity? How and why? |
| 8 | +
|
| 9 | + Write a function to determine if a given target is in the array. |
| 10 | + */ |
| 11 | +public class SearchinRotatedSortedArrayII { |
| 12 | + |
| 13 | + public boolean search(int[] A, int target) { |
| 14 | + int len = A.length; |
| 15 | + if (len == 0) |
| 16 | + return false; |
| 17 | + if (len == 1) { |
| 18 | + if (A[0] == target) { |
| 19 | + return true; |
| 20 | + } else { |
| 21 | + return false; |
| 22 | + } |
| 23 | + } |
| 24 | + int watershed = A[0]; |
| 25 | + int watershedIndex = 0; |
| 26 | + for (int i = 0; i < len - 1; i++) { |
| 27 | + if (A[i] > A[i + 1]) { |
| 28 | + watershed = A[i]; |
| 29 | + watershedIndex = i; |
| 30 | + System.out.println("Place 1: watershed = " + watershed |
| 31 | + + "\twatershedIndex = " + watershedIndex); |
| 32 | + for (int j = i + 1; j < len; j++) { |
| 33 | + if (A[j] == A[i]) { |
| 34 | + watershed = A[j]; |
| 35 | + watershedIndex = j; |
| 36 | + System.out.println("Place 2: watershed = " + watershed |
| 37 | + + "\twatershedIndex = " + watershedIndex); |
| 38 | + } else { |
| 39 | + break; |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + System.out.println("watershed = " + watershed + "\twatershedIndex = " |
| 45 | + + watershedIndex); |
| 46 | + if (target == watershed) |
| 47 | + return true; |
| 48 | + else if (target > watershed) { |
| 49 | + /* |
| 50 | + * here is the tricky part: when target is greater than watershed, |
| 51 | + * it's also possible that this list is ZERO rotated, i.e. it didn't |
| 52 | + * rotate at all! Then at this moment, watershed is not the largest |
| 53 | + * element int this array, so we need to binary search this whole |
| 54 | + * array. |
| 55 | + */ |
| 56 | + if (watershedIndex == 0) { |
| 57 | + int start = 0; |
| 58 | + int end = len - 1; |
| 59 | + int mid = (start + end) / 2; |
| 60 | + while (start <= end) { |
| 61 | + if (target > A[mid]) { |
| 62 | + start = mid + 1; |
| 63 | + mid = (start + end) / 2; |
| 64 | + } else if (target < A[mid]) { |
| 65 | + end = mid - 1; |
| 66 | + mid = (start + end) / 2; |
| 67 | + } else if (target == A[mid]) { |
| 68 | + return true; |
| 69 | + } |
| 70 | + } |
| 71 | + return false; |
| 72 | + } else |
| 73 | + return false; |
| 74 | + } else if (target < watershed) { |
| 75 | + /* |
| 76 | + * target could be in either part of this sorted array, then we |
| 77 | + * check if target is greater than A[0], if so, then search in the |
| 78 | + * first part, if not, then check if it is greater than A[len - 1], |
| 79 | + * if so, return -1, if not, search in the second part |
| 80 | + */ |
| 81 | + |
| 82 | + if (target == A[0]) { |
| 83 | + return true; |
| 84 | + } else if (target > A[0]) { |
| 85 | + int start = 1; |
| 86 | + int end = watershedIndex - 1; |
| 87 | + int mid = (start + end) / 2; |
| 88 | + while (start <= end) { |
| 89 | + if (target > A[mid]) { |
| 90 | + start = mid + 1; |
| 91 | + mid = (start + end) / 2; |
| 92 | + } else if (target < A[mid]) { |
| 93 | + end = mid - 1; |
| 94 | + mid = (start + end) / 2; |
| 95 | + } else if (target == A[mid]) { |
| 96 | + return true; |
| 97 | + } |
| 98 | + } |
| 99 | + return false; |
| 100 | + } else if (target < A[0]) { |
| 101 | + if (target == A[len - 1]) { |
| 102 | + return true; |
| 103 | + } else if (target > A[len - 1]) { |
| 104 | + return false; |
| 105 | + } else if (target < A[len - 1]) { |
| 106 | + int start = watershedIndex + 1; |
| 107 | + int end = len - 2; |
| 108 | + int mid = (start + end) / 2; |
| 109 | + while (start <= end) { |
| 110 | + if (target > A[mid]) { |
| 111 | + start = mid + 1; |
| 112 | + mid = (start + end) / 2; |
| 113 | + } else if (target < A[mid]) { |
| 114 | + end = mid - 1; |
| 115 | + mid = (start + end) / 2; |
| 116 | + } else if (target == A[mid]) { |
| 117 | + return true; |
| 118 | + } |
| 119 | + } |
| 120 | + return false; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments