LeetCode(154) Find Minimum in Rotated Sorted Array II


タイトルは以下の通りです.
Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. The array may contain duplicates.
次のように分析されます.
表面的には前のタイトルのFind Minimm in Rotated Sorted Arrayのように見えますが、変形した二分で探せばいいです.実際には細部があまり考えられないことがあります.
1 midと両端が等しい場合、どの半分を捨てるべきですか?答えは,一度に半分は捨てられず,要素ごとに捨てるしかないという場合,最悪時間複雑度はO(N)である.公式サイトの解答はこう書いてあります.
For case where AL == AM == AR, the minimum could be on AM’s left or right side (eg, [1, 1, 1, 0, 1] or [1, 0, 1, 1, 1]). In this case, we could not discard either subarrays and therefore such worst case degenerates to the order of O(n).
2 while(startネット上のいくつかの答えの書き方がwhile(startマイコード:
左耳のネズミの解法を修正しました.
//derived from haoel 
//12ms
class Solution {
public:
int findMin(vector<int> &num) {
    int start = 0, end = num.size() - 1, mid = 0;
    while (start < end) { 
        while (start < end && num[start] == num[end]) //  ,        !
            ++start; 
        mid  = start + (end - start)/2;
        if (num[start] <= num[mid] && num[mid] <= num[end]) //          
            return num[start];
        if (num[start] > num[mid]) { // Find Minimum in Rotated Sorted Array I     
            end = mid;
        } else if (num[mid] > num[end]) { // Find Minimum in Rotated Sorted Array I     
            start = mid + 1;
        }
    }
    return num[start] < num[end] ? num[start] : num[end]; //     ,   1     2   。
    }
};

参考資料:
1 https://github.com/haoel/leetcode/blob/master/src/findMinimumInRotatedSortedArray/findMinimumInRotatedSortedArray.II.cpp