Problem 153

Abstract / Summary

Find the smallest number in a rotated sorted list of numbers.

Note

The full problem description, including helpful examples, can be found on LeetCode.

C++ 0153.cpp

class Solution {
public:
    int findMin(vector<int>& nums) {
        // brute-force
        return *min_element(nums.begin(), nums.end());
    }
};
7 lines | 156 bytes | submitted August 7, 2023 | GitHub   16% comment ratio | tab size 4

Difficulty

This problem is ranked as
easy medium hard

and has an acceptance rate of about 55% (5+ million submissions).

Typical patterns to solve this problem:
  • Array
  • Binary Search
I solved 663 easy, 1250 medium and 309 hard problems

AI Disclaimer

Every single line of code was written by me, 100% from scratch, between August 2023 and August 2024.
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.

See this dedicated page for more details.
LeetCode profile | GitHub repo

Code Walkthrough

Overview

The presented Solution class offers a single public method, findMin, which determines the smallest integer in a given vector<int>. The implementation relies on the C++ Standard Library algorithm std::min_element, providing a concise and expressive solution.

Core Logic

The method receives its input by reference, avoiding an unnecessary copy of the entire container. Inside the function, the call to std::min_element traverses the range defined by nums.begin() and nums.end(). The algorithm returns an iterator pointing to the element with the minimal value, and the dereference operator yields the integer itself, which is then returned directly.

int findMin(vector<int>& nums) {
    return *min_element(nums.begin(), nums.end());
}

Edge-Case Handling

Because the algorithm operates on iterators, it naturally accommodates vectors of any length greater than zero, including those containing a single element. Duplicate values are handled correctly; the iterator returned points to the first occurrence of the minimal value, ensuring deterministic behavior. The method also works seamlessly with negative numbers, zero, and the full range of int values, as std::min_element performs a straightforward comparison without assumptions about the sign or magnitude of the elements.

Performance Characteristics

Utilizing std::min_element leverages the highly tuned implementation provided by the standard library. This implementation typically iterates through the container in a single pass, benefiting from compiler optimizations such as loop unrolling and vectorized instructions where supported. By operating directly on the original vector via references, the code eliminates the overhead of copying data, preserving cache locality and reducing memory traffic. The return of the dereferenced iterator avoids additional indirection, delivering the result with minimal overhead.

Overall, the solution combines expressive standard-library usage with efficient memory handling, resulting in a clean and performant approach to locating the minimum element in a vector of integers.

written by openai gpt

ELI5 - Simplified Explanation

Imagine a long line of numbered cards lying face up on the floor. Each card has a different number written on it, and they are all mixed up. This code acts like a tiny robot that walks from the very first card all the way to the very last card. As the robot walks, it keeps a sharp eye out to see which card has the smallest number. It holds onto that small number in its memory as it continues down the row. By the time the robot reaches the end of the line, it knows exactly which number was the smallest of all the cards it saw. The lines inside the brackets are just the instructions telling the robot how to peek at every single card to find the winner.

written by google gemini

Code Quality

Several LLMs ranked my code quality on a scale from 1 (very poor) to 10 (outstanding):
n = 26 | median = 4 | μ = 4.69 | σ = 2.38

Complexity Analysis

Time: linear
As a rule of thumb, the maximum input size for one second execution time is
  • factorial: up to 10 elements
  • exponential: up to 20 elements
  • polynomial: a few thousand elements
  • loglinear: up to 100,000 elements
  • linear: a few million elements
  • logarithmic: a billion elements
  • constant: practically unlimited
generated automatically

Other Solutions

I wrote a basic crawler to find other solutions to that problem.
Obviously, I did that after I solved the problem myself.

In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.

And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.

(click on a column header to sort the table)
author
author solutions
www AlgoMonster C++ Python Java TS
WalkCCC C++ Python Java
leetcode.ca C++ Python Rust Java JS Go TS
LeetcodeHelp C++ Python Java
MemoryLimitExceeded C++ Python
Progiez C++ Python Java
github AdelDaniel Dart
aylei Rust
begeekmyfriend C
BigEggStudy C#
black-shadows C++
bujosa Rust
charles-wangkai Java
chenyanzhe C++
everthis JS
FelixFan1122 C#
gaosq0604 Python
Garvit244 Python
halfrost Go
haoel C++
JoshCrozier JS
keineahnung2345 C++
Krazune C++
lennylxx C
linouk23 C++
lkwq007 Python
luangong C++
MacJim C++
qiyuangong Python
ruoru Rust
shreymehul C#
suxrobGM C#
trung-hn Python
varunu28 Java
vthwang Java
wisdompeak C++
withaarzoo C++
XiaotaoGuo C++
YuriSpiridonov Python
zfang399 C++
AlgoMonster C++, Python, Java, TypeScript
WalkCCC C++, Python, Java
leetcode.ca C++, Go, Java, JavaScript, Python, Rust, TypeScript
LeetcodeHelp C++, Java, Python
MemoryLimitExceeded Python, C++
Progiez C++, Java, Python
AdelDaniel Dart
aylei Rust
begeekmyfriend C
BigEggStudy C#
black-shadows C++, C++
bujosa Rust
charles-wangkai Java
chenyanzhe C++
everthis JavaScript
FelixFan1122 C#
gaosq0604 Python
Garvit244 Python
halfrost Go
haoel C++
JoshCrozier JavaScript
keineahnung2345 C++
Krazune C++
lennylxx C
linouk23 C++
lkwq007 Python
luangong C++
MacJim C++
qiyuangong Python
ruoru Rust
shreymehul C#
suxrobGM C#
trung-hn Python
varunu28 Java
vthwang Java
wisdompeak C++
withaarzoo C++
XiaotaoGuo C++
YuriSpiridonov Python
zfang399 C++
56 links | 40 authors | 10 languages

Videos

These are a few popular YouTube videos for this problem:
YouTube video 'LeetCode 153. Find Minimum in Rotated Sorted Array (Algorithm Explained)'
Nick White 10 mins
LeetCode 153. Find Minimum in Rotated Sorted Array (Algorithm Explained)
YouTube video 'Find Minimum in Rotated Sorted Array | Leetcode 153 | codestorywithMIK'
codestorywithMIK 15 mins
Find Minimum in Rotated Sorted Array | Leetcode 153 | codestorywithMIK
YouTube video 'Find Minimum in Rotated Sorted Array - Leetcode 153 - Binary Search (Python)'
Greg Hogg 10 mins
Find Minimum in Rotated Sorted Array - Leetcode 153 - Binary Search (Python)
YouTube video 'Find minimum in rotated sorted array (LeetCode 153) | Full solution with visuals | Divide & Conquer'
Nikhil Lohia 13 mins
Find minimum in rotated sorted array (LeetCode 153) | Full solution with visuals | Divide & Conquer
YouTube video '[Java] Leetcode 153. Find Minimum in Rotated Sorted Array [Binary Search #1]'
Eric Programming 7 mins
[Java] Leetcode 153. Find Minimum in Rotated Sorted Array [Binary Search #1]
YouTube video 'Find Minimum In Rotated Sorted Array - LeetCode 153 - JavaScript'
AlgoJS 5 mins
Find Minimum In Rotated Sorted Array - LeetCode 153 - JavaScript
YouTube video 'Find Minimum in Rotated Sorted Array - LeetCode 153 - Binary Search'
One Code Man 5 mins
Find Minimum in Rotated Sorted Array - LeetCode 153 - Binary Search
YouTube video 'FIND MINIMUM IN ROTATED SORTED ARRAY | LEETCODE # 153 | PYTHON BINARY SEARCH SOLUTION'
Cracking FAANG 16 mins
FIND MINIMUM IN ROTATED SORTED ARRAY | LEETCODE # 153 | PYTHON BINARY SEARCH SOLUTION
YouTube video 'Find Minimum in Rotated Sorted Array| Leetcode 153 | Live coding session  🔥🔥🔥'
Coding Decoded 10 mins
Find Minimum in Rotated Sorted Array| Leetcode 153 | Live coding session 🔥🔥🔥
YouTube video 'LeetCode Search in Rotated Sorted Array | Find Minimum in Rotated Sorted Array'
happygirlzt 11 mins
LeetCode Search in Rotated Sorted Array | Find Minimum in Rotated Sorted Array
YouTube video 'LeetCode Search in Rotated Sorted Array | Find Minimum in Rotated Sorted Array Chinese'
happygirlzt 11 mins
LeetCode Search in Rotated Sorted Array | Find Minimum in Rotated Sorted Array Chinese
YouTube video 'Leetcode | 153. Find Minimum in Rotated Sorted Array | Medium | Java Solution'
Developer Docs 8 mins
Leetcode | 153. Find Minimum in Rotated Sorted Array | Medium | Java Solution
YouTube video 'Can You Find The Minimum Number? (LeetCode 153: Find Minimum in Rotated Sorted Array)'
Code With Zi 11 mins
Can You Find The Minimum Number? (LeetCode 153: Find Minimum in Rotated Sorted Array)
YouTube video 'Daily Leetcode #2236: May 15, 2026 - 153. Find Minimum in Rotated Sorted Array'
Programming Live with Larry 12 mins
Daily Leetcode #2236: May 15, 2026 - 153. Find Minimum in Rotated Sorted Array
YouTube video 'Daily Leetcode #518: Aug 31, 2021 - 153. Find Minimum in Rotated Sorted Array'
Programming Live with Larry 12 mins
Daily Leetcode #518: Aug 31, 2021 - 153. Find Minimum in Rotated Sorted Array
YouTube video '153. Find Minimum in Rotated Sorted Array | Leetcode | Arrays | Fundamentals'
Coding with Tamilarasan 7 mins
153. Find Minimum in Rotated Sorted Array | Leetcode | Arrays | Fundamentals
YouTube video 'LeetCode 153: Find Minimum in Rotated Sorted Array | C# Solution | Binary Search |'
Code Scribbler 6 mins
LeetCode 153: Find Minimum in Rotated Sorted Array | C# Solution | Binary Search |
YouTube video 'Leetcode 153 || Find Minimum in Rotated Sorted Array|| Medium || Binary Search || cpp || Blind 75'
Komal Pal 16 mins
Leetcode 153 || Find Minimum in Rotated Sorted Array|| Medium || Binary Search || cpp || Blind 75
YouTube video 'Find Minimum in Rotated Sorted Array | LeetCode 153 | Coders Camp'
Coders Camp 15 mins
Find Minimum in Rotated Sorted Array | LeetCode 153 | Coders Camp
YouTube video 'LeetCode 153: Find Minimum in Rotated Sorted Array | C# Solution | Binary Search |'
Code Scribbler 12 mins
LeetCode 153: Find Minimum in Rotated Sorted Array | C# Solution | Binary Search |
20 videos   did my crawler come up with invalid videos ? drop me a line.

Similar Problems

1 similar problem

Solved On The Same Day

40 easy 30 medium 1 hard
72 solutions   August 7, 2023

Extra

There are a few extra pages giving you some insights into my setup:
  • Tech Stack - my development environment and tools
  • AI - how AI helped me create this website (NOT solving the problems)
  • Progress - progression over time
  • Quality - code quality