Skip to content

Commit 418f1ab

Browse files
update 33
1 parent e6b1210 commit 418f1ab

File tree

2 files changed

+41
-117
lines changed

2 files changed

+41
-117
lines changed

Diff for: src/main/java/com/fishercoder/solutions/_33.java

+41-87
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,42 @@
11
package com.fishercoder.solutions;
22

3+
/**
4+
* 33. Search in Rotated Sorted Array
5+
* <p>
6+
* There is an integer array nums sorted in ascending order (with distinct values).
7+
* Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length)
8+
* such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed).
9+
* For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].
10+
* Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.
11+
* <p>
12+
* You must write an algorithm with O(log n) runtime complexity.
13+
* <p>
14+
* Example 1:
15+
* Input: nums = [4,5,6,7,0,1,2], target = 0
16+
* Output: 4
17+
* <p>
18+
* Example 2:
19+
* Input: nums = [4,5,6,7,0,1,2], target = 3
20+
* Output: -1
21+
* <p>
22+
* Example 3:
23+
* Input: nums = [1], target = 0
24+
* Output: -1
25+
* <p>
26+
* Constraints:
27+
* 1 <= nums.length <= 5000
28+
* -104 <= nums[i] <= 104
29+
* All values of nums are unique.
30+
* nums is an ascending array that is possibly rotated.
31+
* -104 <= target <= 104
32+
*/
333
public class _33 {
434

535
public static class Solution1 {
6-
public int search(int[] nums, int target) {
7-
if (nums == null || nums.length == 0) {
8-
return -1;
9-
}
10-
int minIdx = findMinIdx(nums);
11-
if (target == nums[minIdx]) {
12-
return minIdx;
13-
}
14-
int m = nums.length;
15-
int start = (target <= nums[m - 1]) ? minIdx : 0;
16-
int end = (target > nums[m - 1]) ? minIdx : m - 1;
17-
18-
while (start <= end) {
19-
int mid = start + (end - start) / 2;
20-
if (nums[mid] == target) {
21-
return mid;
22-
} else if (target > nums[mid]) {
23-
start = mid + 1;
24-
} else {
25-
end = mid - 1;
26-
}
27-
}
28-
return -1;
29-
}
30-
31-
private int findMinIdx(int[] nums) {
32-
int start = 0;
33-
int end = nums.length - 1;
34-
35-
while (start < end) {
36-
int mid = start + (end - start) / 2;
37-
if (nums[mid] > nums[end]) {
38-
start = mid + 1;
39-
} else {
40-
end = mid;
41-
}
42-
}
43-
return start;
44-
}
45-
}
46-
47-
public static class Solution2 {
36+
/**
37+
* Credit: https://leetcode.com/problems/search-in-rotated-sorted-array/editorial/
38+
* Approach 3 says it very well.
39+
*/
4840
public int search(int[] nums, int target) {
4941
if (nums == null || nums.length == 0) {
5042
return -1;
@@ -58,12 +50,19 @@ public int search(int[] nums, int target) {
5850
}
5951

6052
if (nums[left] <= nums[mid]) {
53+
//this is for this case: [4, 5, 6, 7, 0, 1, 2], target = 4
54+
//this means that the left sub-array is sorted
6155
if (target >= nums[left] && target < nums[mid]) {
56+
//in this case, if target exists, in must be in this left sorted sub-array
6257
right = mid - 1;
6358
} else {
59+
//otherwise, it's in the other half
60+
//e.g. this case: [4, 5, 6, 7, 0, 1, 2], target = 2
6461
left = mid + 1;
6562
}
6663
} else {
64+
//this is for this case: [8, 9, 2, 3, 4], target = 9
65+
//this means the right sub-array is sorted and the left sub-array is rotated at some pivot
6766
if (target > nums[mid] && target <= nums[right]) {
6867
left = mid + 1;
6968
} else {
@@ -74,49 +73,4 @@ public int search(int[] nums, int target) {
7473
return nums[left] == target ? left : -1;
7574
}
7675
}
77-
78-
public static class Solution3 {
79-
/**
80-
* My completely original solution on 10/23/2021, although spaghetti code.
81-
*/
82-
public int search(int[] nums, int target) {
83-
int left = 0;
84-
int right = nums.length - 1;
85-
while (left < right) {
86-
if (nums[left] == target) {
87-
return left;
88-
} else if (nums[right] == target) {
89-
return right;
90-
}
91-
int mid = left + (right - left) / 2;
92-
if (left == mid || right == mid) {
93-
break;
94-
}
95-
if (nums[mid] == target) {
96-
return mid;
97-
} else if (nums[mid] > target && nums[right] > target && nums[left] > target) {
98-
if (nums[right] < nums[mid]) {
99-
left = mid + 1;
100-
} else {
101-
right = mid - 1;
102-
}
103-
} else if (nums[mid] > target && nums[right] < target) {
104-
right = mid - 1;
105-
} else if (nums[mid] < target && nums[right] > target) {
106-
left = mid + 1;
107-
} else if (nums[mid] < target && nums[right] < target && nums[left] < target) {
108-
if (nums[mid] < nums[left] && nums[mid] < nums[right]) {
109-
right = mid - 1;
110-
} else {
111-
left = mid + 1;
112-
}
113-
} else if (nums[mid] > target && nums[left] < target) {
114-
right = mid;
115-
} else if (nums[mid] < target && nums[right] < target && nums[left] > target) {
116-
right = mid - 1;
117-
}
118-
}
119-
return (right >= 0 && nums[right] == target) ? right : (nums[left] == target) ? left : -1;
120-
}
121-
}
12276
}

Diff for: src/test/java/com/fishercoder/_33Test.java

-30
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@
88

99
public class _33Test {
1010
private static _33.Solution1 solution1;
11-
private static _33.Solution2 solution2;
12-
private static _33.Solution3 solution3;
1311
private static int[] nums;
1412
private static int expected;
1513
private static int target;
1614

1715
@BeforeEach
1816
public void setup() {
1917
solution1 = new _33.Solution1();
20-
solution2 = new _33.Solution2();
21-
solution3 = new _33.Solution3();
2218
}
2319

2420
@Test
@@ -27,8 +23,6 @@ public void test1() {
2723
expected = 3;
2824
target = 7;
2925
assertEquals(expected, solution1.search(nums, target));
30-
assertEquals(expected, solution2.search(nums, target));
31-
assertEquals(expected, solution3.search(nums, target));
3226
}
3327

3428
@Test
@@ -37,8 +31,6 @@ public void test2() {
3731
expected = 4;
3832
target = 0;
3933
assertEquals(expected, solution1.search(nums, target));
40-
assertEquals(expected, solution2.search(nums, target));
41-
assertEquals(expected, solution3.search(nums, target));
4234
}
4335

4436
@Test
@@ -47,8 +39,6 @@ public void test3() {
4739
expected = 1;
4840
target = 5;
4941
assertEquals(expected, solution1.search(nums, target));
50-
assertEquals(expected, solution2.search(nums, target));
51-
assertEquals(expected, solution3.search(nums, target));
5242
}
5343

5444
@Test
@@ -57,8 +47,6 @@ public void test4() {
5747
expected = -1;
5848
target = 3;
5949
assertEquals(expected, solution1.search(nums, target));
60-
assertEquals(expected, solution2.search(nums, target));
61-
assertEquals(expected, solution3.search(nums, target));
6250
}
6351

6452
@Test
@@ -67,8 +55,6 @@ public void test5() {
6755
expected = -1;
6856
target = 0;
6957
assertEquals(expected, solution1.search(nums, target));
70-
assertEquals(expected, solution2.search(nums, target));
71-
assertEquals(expected, solution3.search(nums, target));
7258
}
7359

7460
@Test
@@ -77,8 +63,6 @@ public void test6() {
7763
expected = -1;
7864
target = 4;
7965
assertEquals(expected, solution1.search(nums, target));
80-
assertEquals(expected, solution2.search(nums, target));
81-
assertEquals(expected, solution3.search(nums, target));
8266
}
8367

8468
@Test
@@ -87,8 +71,6 @@ public void test7() {
8771
expected = -1;
8872
target = 6;
8973
assertEquals(expected, solution1.search(nums, target));
90-
assertEquals(expected, solution2.search(nums, target));
91-
assertEquals(expected, solution3.search(nums, target));
9274
}
9375

9476
@Test
@@ -97,8 +79,6 @@ public void test8() {
9779
expected = -1;
9880
target = 2;
9981
assertEquals(expected, solution1.search(nums, target));
100-
assertEquals(expected, solution2.search(nums, target));
101-
assertEquals(expected, solution3.search(nums, target));
10282
}
10383

10484
@Test
@@ -107,8 +87,6 @@ public void test9() {
10787
expected = -1;
10888
target = 4;
10989
assertEquals(expected, solution1.search(nums, target));
110-
assertEquals(expected, solution2.search(nums, target));
111-
assertEquals(expected, solution3.search(nums, target));
11290
}
11391

11492
@Test
@@ -117,8 +95,6 @@ public void test10() {
11795
expected = 3;
11896
target = 4;
11997
assertEquals(expected, solution1.search(nums, target));
120-
assertEquals(expected, solution2.search(nums, target));
121-
assertEquals(expected, solution3.search(nums, target));
12298
}
12399

124100
@Test
@@ -127,8 +103,6 @@ public void test11() {
127103
expected = 1;
128104
target = 1;
129105
assertEquals(expected, solution1.search(nums, target));
130-
assertEquals(expected, solution2.search(nums, target));
131-
assertEquals(expected, solution3.search(nums, target));
132106
}
133107

134108
@Test
@@ -137,8 +111,6 @@ public void test12() {
137111
expected = 1;
138112
target = 9;
139113
assertEquals(expected, solution1.search(nums, target));
140-
assertEquals(expected, solution2.search(nums, target));
141-
assertEquals(expected, solution3.search(nums, target));
142114
}
143115

144116
@Test
@@ -147,8 +119,6 @@ public void test13() {
147119
expected = 4;
148120
target = 8;
149121
assertEquals(expected, solution1.search(nums, target));
150-
assertEquals(expected, solution2.search(nums, target));
151-
assertEquals(expected, solution3.search(nums, target));
152122
}
153123

154124
}

0 commit comments

Comments
 (0)