Skip to content

Commit 996ba3f

Browse files
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 14.1 MB (45.77%)
1 parent 81ac146 commit 996ba3f

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<p>Suppose an array of length <code>n</code> sorted in ascending order is <strong>rotated</strong> between <code>1</code> and <code>n</code> times. For example, the array <code>nums = [0,1,2,4,5,6,7]</code> might become:</p>
2+
3+
<ul>
4+
<li><code>[4,5,6,7,0,1,2]</code> if it was rotated <code>4</code> times.</li>
5+
<li><code>[0,1,2,4,5,6,7]</code> if it was rotated <code>7</code> times.</li>
6+
</ul>
7+
8+
<p>Notice that <strong>rotating</strong> an array <code>[a[0], a[1], a[2], ..., a[n-1]]</code> 1 time results in the array <code>[a[n-1], a[0], a[1], a[2], ..., a[n-2]]</code>.</p>
9+
10+
<p>Given the sorted rotated array <code>nums</code> of <strong>unique</strong> elements, return <em>the minimum element of this array</em>.</p>
11+
12+
<p>You must write an algorithm that runs in&nbsp;<code>O(log n) time</code>.</p>
13+
14+
<p>&nbsp;</p>
15+
<p><strong class="example">Example 1:</strong></p>
16+
17+
<pre>
18+
<strong>Input:</strong> nums = [3,4,5,1,2]
19+
<strong>Output:</strong> 1
20+
<strong>Explanation:</strong> The original array was [1,2,3,4,5] rotated 3 times.
21+
</pre>
22+
23+
<p><strong class="example">Example 2:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> nums = [4,5,6,7,0,1,2]
27+
<strong>Output:</strong> 0
28+
<strong>Explanation:</strong> The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
29+
</pre>
30+
31+
<p><strong class="example">Example 3:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong> nums = [11,13,15,17]
35+
<strong>Output:</strong> 11
36+
<strong>Explanation:</strong> The original array was [11,13,15,17] and it was rotated 4 times.
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
<p><strong>Constraints:</strong></p>
41+
42+
<ul>
43+
<li><code>n == nums.length</code></li>
44+
<li><code>1 &lt;= n &lt;= 5000</code></li>
45+
<li><code>-5000 &lt;= nums[i] &lt;= 5000</code></li>
46+
<li>All the integers of <code>nums</code> are <strong>unique</strong>.</li>
47+
<li><code>nums</code> is sorted and rotated between <code>1</code> and <code>n</code> times.</li>
48+
</ul>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
int findMin(vector<int>& nums) {
4+
return *min_element(nums.begin(), nums.end());
5+
}
6+
};

0 commit comments

Comments
 (0)