Line data Source code
1 : // Copyright (c) 2022 The Authors. All rights reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // https://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : // Authors: liubang (it.liubang@gmail.com)
16 : // Created: 2022/01/17 14:50
17 :
18 : #include <gtest/gtest.h>
19 :
20 : #include <vector>
21 :
22 : namespace {
23 : class Solution {
24 : public:
25 2 : int findMin(const std::vector<int>& nums) {
26 2 : int s = 0, e = nums.size() - 1;
27 6 : while (s < e) {
28 4 : int m = (s + e) / 2;
29 4 : if (nums[m] > nums[e]) {
30 1 : s = m + 1;
31 3 : } else if (nums[m] < nums[e]) {
32 3 : e = m;
33 : } else {
34 0 : e--;
35 : }
36 : }
37 2 : return nums[s];
38 : }
39 : };
40 : } // namespace
41 :
42 4 : TEST(Leetcode, find_minimum_in_rotated_sorted_array_ii) {
43 1 : Solution s;
44 1 : EXPECT_EQ(1, s.findMin({1, 3, 5}));
45 1 : EXPECT_EQ(0, s.findMin({2, 2, 2, 0, 1}));
46 1 : }
|