Python 2848.py
class Solution:
def numberOfPoints(self, nums: List[List[int]]) -> int:
result = 0
for i in range(1, 100 + 1): # start & end between 1 and 100
for s, e in nums:
if s <= i <= e:
result += 1
break
return result
Difficulty
and has an acceptance rate of about 73% (100k+ submissions).
AI Disclaimer
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.
Code Walkthrough
Overview
The provided Solution class implements a method numberOfPoints that receives a list of integer intervals (nums). Each interval is represented by a two-element list [start, end]. The method counts how many distinct integer points in the inclusive range 1 to 100 are covered by at least one of the given intervals. The result is returned as an integer.
Key Steps
The method begins by creating a variable to accumulate the count.
result = 0
An outer loop iterates over every integer i from 1 through 100. The comment clarifies that all interval boundaries lie within this range.
for i in range(1, 100 + 1): # start & end between 1 and 100
Inside the outer loop, a second loop walks through each interval s, e in the input list nums. The tuple unpacking extracts the start and end of the current interval.
for s, e in nums:
For each interval the method checks whether the current point i falls inside the interval, using an inclusive comparison.
if s <= i <= e:
When the condition is satisfied, the accumulator is incremented and the inner loop is exited immediately with break. The break ensures that each point contributes at most one unit to the total, regardless of how many intervals cover it.
result += 1
break
After all points have been examined, the final count is returned.
return result
Edge Cases Handled
The algorithm gracefully handles intervals that are completely outside the 1-to-100 domain. Because the outer loop never visits points outside this range, such intervals never affect the result. Intervals that consist of a single point (where start == end) are correctly counted when the loop reaches that exact value. Overlapping intervals are also managed correctly: the break statement stops further checks once the first covering interval is found, preventing double-counting of the same point. An empty nums list leads to no increments, and the method returns zero, which is the appropriate result for a lack of coverage.
Performance Optimizations
The most notable optimization is the early exit from the inner loop via break. As soon as a point is found to be covered, the algorithm stops scanning the remaining intervals for that point, reducing the number of comparisons especially when many intervals overlap. Additionally, the outer loop’s range is fixed to 100 iterations, providing a hard bound on the total work regardless of the size of nums. This deterministic bound keeps the execution time predictable and avoids unnecessary processing beyond the defined domain.
ELI5 - Simplified Explanation
Imagine a long row of one hundred numbered parking spots outside a toy store. Some cars are parked along these spots, and each car takes up a specific range of them, like from spot five to spot ten.
To figure out how many total spots have at least one car in them, the computer walks down the row of spots one by one, starting at the first spot and going all the way to the hundredth spot. For every single spot, the computer looks at all the parked cars to see if any of them are sitting on that one.
As soon as the computer finds even one car covering the current spot, it marks that spot as occupied and then quickly hops over to check the next spot in line. By counting all the spots that had at least one car, the computer knows exactly how many spaces are filled.
Code Quality
Complexity Analysis
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
Other Solutions
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 | Java | Python | C++ | TS | ||
| WalkCCC | Java | Python | C++ | ||||
| leetcode.ca | Java | Python | C++ | Go | TS | ||
| Progiez | Java | Python | C++ | ||||
| github | charles-wangkai | Java | |||||
| fsq | JS | ||||||
| JoshCrozier | JS | ||||||
| lkwq007 | Python | ||||||
| varunu28 | Java | ||||||
| AlgoMonster | C++, Python, Java, TypeScript | ||||||
| WalkCCC | C++, Python, Java | ||||||
| leetcode.ca | C++, Go, Java, Python, TypeScript | ||||||
| Progiez | C++, Java, Python | ||||||
| charles-wangkai | Java | ||||||
| fsq | JavaScript | ||||||
| JoshCrozier | JavaScript | ||||||
| lkwq007 | Python | ||||||
| varunu28 | Java |
Videos
Similar Problems
- problem 56: Merge Intervals
Solved On The Same Day
- problem 953: Verifying an Alien Dictionary
- problem 961: N-Repeated Element in Size 2N Array
- problem 2264: Largest 3-Same-Digit Number in String
- problem 2682: Find the Losers of the Circular Game
- problem 2784: Check if Array is Good
- problem 2869: Minimum Operations to Collect Elements
- problem 890: Find and Replace Pattern
- problem 954: Array of Doubled Pairs
- problem 959: Regions Cut By Slashes
- problem 966: Vowel Spellchecker
- problem 1202: Smallest String With Swaps
Extra
- 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