Skip to content

Commit 68e1390

Browse files
🐎 Day 20
1 parent 4dd9601 commit 68e1390

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
3. [Mirror Reflection](https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/566/week-3-november-15th-november-21st/3534/) ➡️ [CPP Solution](Week3/mirrorReflection.cpp)
2929
4. [Merge Intervals](https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/566/week-3-november-15th-november-21st/3535/) ➡️ [CPP Solution](Week3/merge.cpp)
3030
5. [Decode String](https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/566/week-3-november-15th-november-21st/3536/) ➡️ [CPP Solution](Week3/decodeString.cpp)
31+
6. [Search in Rotated Sorted Array II](https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/566/week-3-november-15th-november-21st/3537/) ➡️ [CPP Solution](Week3/search.cpp)
3132

3233
## Week 4 🚧
3334
Coming soon...

Week3/search.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public:
3+
bool search(vector<int>& nums, int target) {
4+
int n = nums.size();
5+
if(n == 0) return false;
6+
7+
int low = 0, high = n - 1, mid, first = nums[0];
8+
9+
while(low <= high) {
10+
11+
while(low < high && nums[low] == nums[low + 1])
12+
low++;
13+
14+
while(low < high && nums[high] == nums[high - 1])
15+
high--;
16+
17+
18+
mid = low + (high - low) / 2;
19+
int value = nums[mid];
20+
21+
if(value == target) return true;
22+
23+
bool am_big = value >= first;
24+
bool target_big = target >= first;
25+
26+
if(am_big == target_big) {
27+
if(value < target) {
28+
low = mid + 1;
29+
} else {
30+
high = mid - 1;
31+
}
32+
} else {
33+
if(am_big) {
34+
low = mid + 1;
35+
} else {
36+
high = mid - 1;
37+
}
38+
}
39+
}
40+
41+
return false;
42+
}
43+
};

0 commit comments

Comments
 (0)