Skip to content

Commit 4bb7083

Browse files
Problem 33 (#40)
* add solution and resources * update README
1 parent e0dbbb1 commit 4bb7083

File tree

2 files changed

+139
-1
lines changed

2 files changed

+139
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
5050
39. [Validate Binary Search Tree #98](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/validate-binary-tree-98.md)
5151
40. [Number of Islands #200](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/number-of-islands-200.md)
5252
41. [Rotting Oranges #994](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/rotting-oranges-994.md)
53-
42. Search in Rotated Sorted Array #33
53+
42. [Search in Rotated Sorted Array #33](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/search-rotated-array-33.md)
5454
43. Combination Sum #39
5555
44. Permutations #46
5656
45. [Merge Intervals #56](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/merge-intervals-56.md)
@@ -113,6 +113,7 @@ In order to practice with similar data structures I'll be placing each problem i
113113
- [Product of Array Except Self #238](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/product-of-array-238.md)
114114
- [Number of Islands #200](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/number-of-islands-200.md)
115115
- [Rotting Oranges #994](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/rotting-oranges-994.md)
116+
- [Search in Rotated Sorted Array #33](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/search-rotated-array-33.md)
116117

117118
### Queue
118119

@@ -210,6 +211,7 @@ Within the problems above there are several patterns that often occur. I plan to
210211
### Binary Search
211212

212213
- [First Bad Version #278](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/first-bad-version-278.md)
214+
- [Search in Rotated Sorted Array #33](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/search-rotated-array-33.md)
213215

214216
### Breadth First Search
215217

medium/search-rotated-array-33.md

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# Search in Rotated Sorted Array
2+
3+
Page on leetcode: https://leetcode.com/problems/search-in-rotated-sorted-array/
4+
5+
## Problem Statement
6+
7+
There is an integer array nums sorted in ascending order (with distinct values).
8+
9+
Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). 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+
11+
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.
12+
13+
You must write an algorithm with O(log n) runtime complexity.
14+
15+
### Constraints
16+
17+
- 1 <= nums.length <= 5000
18+
- -104 <= nums[i] <= 104
19+
- All values of nums are unique.
20+
- nums is an ascending array that is possibly rotated.
21+
- -104 <= target <= 104
22+
23+
### Example
24+
25+
```
26+
Input: nums = [4,5,6,7,0,1,2], target = 0
27+
Output: 4
28+
```
29+
30+
## Solution
31+
32+
- return index of target
33+
- -1 if no target found
34+
- Must be O(logn)
35+
- [15, 20, 23, 40, 78]
36+
- [40, 78, 15, 20, 23]
37+
38+
### Pseudocode
39+
40+
1. Check if end is less than start, if so it has been rotated
41+
2. If not rotated use binary search (find mid, check mid, iteratively)
42+
3. If rotated check if target is less than end, if true then check in second part of array, if not check in first part
43+
4. Check second part, move start to mid point, check if mid is less than target, if less then proceed with binary search
44+
5. If greater than, move start to midpoint between 0-index and start
45+
46+
### Initial Attempt
47+
48+
```javascript
49+
const search = function (nums, target) {
50+
let start = 0;
51+
let end = nums.length - 1;
52+
53+
if (nums[end] > sums[start]) {
54+
return binarySearch(start, end);
55+
}
56+
57+
// Find which part of rotated array the target is in
58+
if (target < nums[end]) {
59+
start = start + (end - start) / 2;
60+
return findSortedArray(start, end);
61+
} else {
62+
end = start + (end - start) / 2;
63+
findSortedArray(start, end);
64+
}
65+
66+
function findSortedArray(l, r) {
67+
// Proceed with binary search
68+
if (l < target && r > target) {
69+
return binarySearch(l, r);
70+
}
71+
72+
if (l > target && l !== 0) {
73+
l = l / 2;
74+
} else if (r < target && r !== nums.length - 1) {
75+
r = r / 2;
76+
}
77+
}
78+
79+
// binary search helper function
80+
function binarySearch(l, r) {
81+
while (l < r) {
82+
// update mid each loop
83+
const mid = l + (r - l) / 2;
84+
if (nums[mid] > target) {
85+
r = mid - 1;
86+
} else if (nums[mid] < target) {
87+
l = mid + 1;
88+
} else {
89+
return mid;
90+
}
91+
}
92+
// target not found
93+
return -1;
94+
}
95+
};
96+
```
97+
98+
### Optimized Solution
99+
100+
This solution has a time complexity of O(logn) and a space complexity of O(1). You can see an explanation of the solution here: https://www.youtube.com/watch?v=U8XENwh8Oy8
101+
102+
```javascript
103+
const search = function (nums, target) {
104+
let l = 0;
105+
let r = nums.length - 1;
106+
107+
while (l <= r) {
108+
// update mid each loop
109+
const mid = Math.floor((l + r) / 2);
110+
111+
// target found
112+
if (nums[mid] === target) {
113+
return mid;
114+
}
115+
116+
// mid point is on the left sorted portion of the array
117+
if (nums[l] <= nums[mid]) {
118+
if (target > nums[mid] || target < nums[l]) {
119+
l = mid + 1;
120+
} else {
121+
r = mid - 1;
122+
}
123+
} else {
124+
// mid points is on the right sorted portion of the array
125+
if (target < nums[mid] || target > nums[r]) {
126+
r = mid - 1;
127+
} else {
128+
l = mid + 1;
129+
}
130+
}
131+
}
132+
133+
// target not found
134+
return -1;
135+
};
136+
```

0 commit comments

Comments
 (0)