Skip to content

Commit aadb495

Browse files
committedFeb 2, 2025
Add solution #154
1 parent a4efb81 commit aadb495

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
151|[Reverse Words in a String](./0151-reverse-words-in-a-string.js)|Medium|
156156
152|[Maximum Product Subarray](./0152-maximum-product-subarray.js)|Medium|
157157
153|[Find Minimum in Rotated Sorted Array](./0153-find-minimum-in-rotated-sorted-array.js)|Medium|
158+
154|[Find Minimum in Rotated Sorted Array II](./0154-find-minimum-in-rotated-sorted-array-ii.js)|Hard|
158159
155|[Min Stack](./0155-min-stack.js)|Medium|
159160
160|[Intersection of Two Linked Lists](./0160-intersection-of-two-linked-lists.js)|Medium|
160161
162|[Find Peak Element](./0162-find-peak-element.js)|Medium|
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 154. Find Minimum in Rotated Sorted Array II
3+
* https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/
4+
* Difficulty: Hard
5+
*
6+
* Suppose an array of length n sorted in ascending order is rotated between 1 and n times.
7+
* For example, the array nums = [0,1,4,4,5,6,7] might become:
8+
* - [4,5,6,7,0,1,4] if it was rotated 4 times.
9+
* - [0,1,4,4,5,6,7] if it was rotated 7 times.
10+
*
11+
* Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array
12+
* [a[n-1], a[0], a[1], a[2], ..., a[n-2]].
13+
*
14+
* Given the sorted rotated array nums that may contain duplicates, return the minimum element
15+
* of this array.
16+
*
17+
* You must decrease the overall operation steps as much as possible.
18+
*/
19+
20+
/**
21+
* @param {number[]} nums
22+
* @return {number}
23+
*/
24+
var findMin = function(nums) {
25+
for (let i = 0; i < nums.length - 1; i++) {
26+
if (nums[i] > nums[i + 1]) {
27+
return nums[i + 1];
28+
}
29+
}
30+
return nums[0];
31+
};

0 commit comments

Comments
 (0)
Please sign in to comment.