LeetCode #53 Maximum Subarray

The Objective: find the contiguous subarray which has the largest sum and return its sum.

As with any code I write on Leetcode, I time myself and try to solve the problem quickly and go back after my first submission and think about some anlges to come from. in this case I followed the typical brute force methodes as could be seen above. While it's very ineffcient it can find the the correct solutions. When submitted to leetcode the answer timed out, verifying my suspicions.

Improved Code Using Slicing

Once I reviewed my code i realised an immediate mistake and that was not taking advantage of subarray slicing. Another thought was to set a current max to use and only if a subarray increases that max could it replace it. These two in combination provided a successful submission.

Lessons Learned:

I have the habit of trying to brute force my way through problems. I need to start by thinking about problems beyond just the typical quadratic format. Another remider is to take advantage of python sub array slicing. So After this answer I did a little review of how and when to use it.

Image placeholder

Stuart Ross

A Data Science Intern at Leeds Institute of Data Analytics. This was completed in my spare time as an independent learning oppertunity... and honestly just for a bit of fun too.