Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Time Limit Exceeded error
#3
One other thing. This is not important for this challenge because your solution will not have loops inside loops, but there is a better way to do the looping you were doing in your OP.
class Solution:
    def maxArea(self, height):
        result = 0
        for i in range(len(height)-1):
            for j in range(i+1, len(height)):
                result = max(result, min(height[i], height[j]) * (j - i))
        return result            
This shaves about 10 seconds off your code. This makes sense because the iterations are cut in half.
loves likes this post
Reply


Messages In This Thread
Time Limit Exceeded error - by loves - Dec-02-2020, 09:40 PM
RE: Time Limit Exceeded error - by deanhystad - Dec-02-2020, 11:31 PM
RE: Time Limit Exceeded error - by Sofia_Grace - Dec-03-2020, 07:15 AM
RE: Time Limit Exceeded error - by deanhystad - Dec-02-2020, 11:40 PM
RE: Time Limit Exceeded error - by loves - Dec-02-2020, 11:47 PM
RE: Time Limit Exceeded error - by deanhystad - Dec-03-2020, 06:11 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Plotting by Time, error mansoorahs 1 811 May-16-2023, 09:46 AM
Last Post: Larz60+
  Assign a value if datetime is in between a particular time limit klllmmm 2 2,853 Jan-02-2021, 07:00 AM
Last Post: klllmmm
  Time conversion error tester_V 1 2,102 Oct-28-2020, 10:48 PM
Last Post: tester_V
Bug maximum recursion depth exceeded while calling a Python object error in python3 Prezess 4 3,904 Aug-02-2020, 02:21 PM
Last Post: deanhystad
  Read CSV error: python KeyError: 'Time' charlicruz 1 5,337 Jun-27-2020, 09:56 AM
Last Post: charlicruz
  Error when running the second time metro17 3 3,879 Aug-30-2019, 12:09 PM
Last Post: ThomasL
  RecursionError: maximum recursion depth exceeded in comparison ? leoahum 11 13,346 Mar-18-2019, 01:53 PM
Last Post: leoahum
  fibonacci ***Time limit exceeded*** frequency 18 10,626 Nov-29-2018, 09:03 PM
Last Post: frequency
  pyad time conversion error (plus solution) PapaZod 2 3,623 Nov-25-2018, 02:42 PM
Last Post: PapaZod
  'Time Limit Exceeded' Problem bkpee3 2 5,603 Nov-14-2018, 03:51 AM
Last Post: bkpee3

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020