Python code for serving k customers with bounded time window As I asked a question here, I want to write a python code for the following problem:
A person provides a service and he/she can serve \$k\$ clients each minute. Now, client number \$i\$ comes at the beginning of minute \$ a_{i} \$ and waits \$w_{i}\$ minutes to receive the service and if they don't receive the service in the interval \$[a_{i}, a_{i} + w_{i}]\$, they leave. If we have \$n\$ clients, can the person serve all them or not?
Now, by the answer stated there, I wrote the following code.
intervals = [] # Some intervals like [[1, 2], [1, 100], [1, 1], [5, 20]]

intervals = sorted(intervals)

start = intervals[0][0]
end = intervals[-1][1]

i = start
while i &lt; end:
    if not intervals:
        break
    else:
        for j in range(k):
            try:
                if intervals[0][0] &lt;= i &lt;= intervals[0][1]:
                    del intervals[0]
                else:
                    break
            except:
                pass
    i += 1

if not intervals:
    print('YES')
else:
    print('NO')

My question: Is my code right? Can it become more efficient?
Time complexity is unnecessarily large.
First, it is linear in the timespan of the arrivals. A list [[1,2], [100,101]] would require 100 iterations; a list [1,2], [1000,1001]] would require 1000 iterations, etc. Don't bruteforce every minute.
Second, del intervals[0] has a linear time complexity in number of intervals, which results in an overall quadratic time complexity. A cheap way out is to sort in a descending order, and work right to left. This way you would remove intervals from the tail of the list, which is O(1).

DRY. A presence of two semantically identical breaks (the loop is broken when it runs out of the intervals) suggests refactoring. Do not test for if not intervals explicitly. Consider
  try:
      while i &lt; end:
          for j in range(k):
              if intervals[0][0] &lt;=i &lt;= intervals[0][1]:
                  del intervals[0]
          i += 1
  except:
      pass

Of course, never except blindly. Catch only exceptions you expect and know how to handle, in this case except IndexError:.