Search in Rotated Sorted Array II
2013.12.26 20:07
Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given target is in the array.
Solution1:
First comes the link to the old original problem: Search in Rotated Sorted Array.
The first solution wasn't quite satisfactory, with a time complexity of O(n). First we find out the rotation offset with O(n) time, and do a rotated binary search with another O(log(n)) time. Space complexity is O(1).
Accepted code:
1 // 1AC, simple variation of binary search, even simpler 2 class Solution { 3 public: 4 bool search(int A[], int n, int target) { 5 // Note: The Solution object is instantiated only once and is reused by each test case. 6 int offset; 7 8 if(A == nullptr || n <= 0){ 9 return false; 10 } 11 12 for(offset = 0; offset < n; ++offset){ 13 if(A[offset] > A[(offset + 1) % n]){ 14 break; 15 } 16 } 17 18 int left, mid, right; 19 20 offset = (offset + 1) % n; 21 left = offset; 22 right = n - 1 + offset; 23 while(left <= right){ 24 mid = (left + right) / 2; 25 if(target < A[mid % n]){ 26 right = mid - 1; 27 }else if(target > A[mid % n]){ 28 left = mid + 1; 29 }else{ 30 return true; 31 } 32 } 33 34 return false; 35 } 36 };
Solution2:
Think about this case: [2, 2, 2, 2, 0, 2, 2]
If we apply the previous O(log(n)) solution from the original problem, there will be a problem:
1. left = 0;
2. right = 6;
3. mid = 3;
4. A[left] == A[mid], A[mid] == A[right]
Then, how do I find out the rotation offset using bi-search if I'm unable to decide which path to go next?
Thus we can apply binary search on an interval only when it is strictly ascending, use linear search otherwise.
Time complexity is between O(log(n)) and O(n), space complexity is O(1).
Accepted code:
1 // 1AC, not as efficient, but good solution 2 class Solution { 3 public: 4 bool search(int A[], int n, int target) { 5 // Note: The Solution object is instantiated only once and is reused by each test case. 6 int offset; 7 8 if(A == nullptr || n <= 0){ 9 return false; 10 } 11 12 int left, mid, right; 13 14 left = 0; 15 right = n - 1; 16 while(left <= right){ 17 mid = (left + right) / 2; 18 // normal binary search cannot deal with cases like this 19 // [2, 2, 0, 0, 2, 2, 2] 20 if(A[left] < target && target < A[mid]){ 21 right = mid - 1; 22 }else if(A[mid] < target && target < A[right]){ 23 left = mid + 1; 24 }else{ 25 if(A[left] == target){ 26 return true; 27 }else{ 28 ++left; 29 } 30 if(A[right] == target){ 31 return true; 32 }else{ 33 --right; 34 } 35 } 36 } 37 38 return false; 39 } 40 };
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)