Problem overview
The function sumUntilLimit receives a positive integer limit. It adds the numbers 1, 2, 3, and so on, in that order, and stops as soon as the total is at least limit. It returns how many numbers it added. The total may go past the limit: the number that makes it reach or pass the limit counts too.
For limit = 10, 1 + 2 + 3 + 4 is exactly 10, so the answer is 4. For limit = 11, the total after 4 numbers is still 10, so 5 is added as well, the total becomes 15, and the answer is 5. For limit = 1 the first number is enough, and the answer is 1.
Approach
Two values change as the loop runs: the total so far, and how many numbers have been added. Because the numbers go 1, 2, 3, in order, the count is also the last number added: after adding 1, 2 and 3, the count is 3.
That lets one variable do two jobs. Each pass raises count by 1 to get the next number, then adds count to total. The loop keeps going while total < limit, so it stops on the first pass where the total reaches the limit, and the number added on that pass is included in the count.
Step by step
- Start
totalat 0. Nothing has been added yet. - Start
countat 0. It counts the numbers added so far. - Loop while
total < limit. This is the exact opposite of the stopping rule "at least the limit". - Add 1 to
count. It becomes the next number to add: 1 on the first pass, 2 on the second. - Add
counttototal. The order matters:countwent up first, so the number added is the right one. - Return
countafter the loop.
On limit = 11:
| Pass | count (number added) | total |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 3 |
| 3 | 3 | 6 |
| 4 | 4 | 10 |
| 5 | 5 | 15 |
After pass 4 the total, 10, is still below 11, so pass 5 runs. After it the total is 15, the condition fails, and the function returns 5.
The solution in Python
class Solution:
def sumUntilLimit(self, limit: int) -> int:
total = 0
count = 0
while total < limit:
count += 1
total += count
return count
Details worth knowing:
- The loop checks first. A
whileloop tests its condition before every pass, including the first. Withtotalat 0 and alimitof at least 1, the first test is always true. - Why
<and not<=. The task stops when the total is at leastlimit. The loop continues while the opposite holds: the total is below it. Withtotal <= limit, the loop would add one more number when the total equals the limit. For 10 it would return 5, not 4. - Don't confuse the two variables. The function returns
count, the number of terms, nottotal. For a limit of 11 the total is 15 but the answer is 5. - Large limits are fast enough. For
limit = 1000000000the loop runs 44721 times, which is quick.
Edge cases and common mistakes
- Limit 1. One pass:
countis 1,totalis 1, and the loop ends. The answer is 1. - Limit 2 and 3. Both give 2, because 1 + 2 is 3, which reaches both limits.
- Swapping the two body lines. Adding
counttototalbefore raisingcountadds 0 on the first pass and every number one pass late, so the answer comes out wrong.
Testing
10gives4.11gives5.1gives1.2gives2.3gives2.1000000000gives44721.
