Maximum Subarray Product Performance: Divide and Conquer vs Kadane's Algorithm
https://cstheory.stackexchange.com/questions/19449/algorithm-for-max-network-flow-with-lower-bounds-and-its-complexity

Maximum Subarray Product Performance: Divide and Conquer vs Kadane's Algorithm

Original leetcode article is here.

I recently came across this list of problems to study from to become a better programmer. This is quite a famous list in the industry and a favorite for a lot of people preparing for software engineering interviews at FAANG companies (Facebook, Amazon, Apple, Netflix and Google).

Anyhow, I ran into issues pretty much from the get-go. Some patterns were easier to learn than others (so far). However, the problem that gave me a headache since yesterday evening is this one. This is a `maximum subarray product` problem that can have an arbitrary number of elements containing 0s, negative and positive numbers. While it was relatively simple to understand the problem conceptually, it was surprisingly hard to implement a solution.

Now, my approach to solve a problem is to do it iteratively. I typically open up a Python console and type up a solution that I think should work. I then run it with a few example inputs, as varied and edgy as I can conjure, and look at the resulting errors and go from there. Once I am satisfied with the solution, I look around in the code to improve readability and reduce jumps, if possible.

Trying to do this I realised that this problem can be solved by the divide and conquer method as follows:

  1. Divide the array into subarrays whenever a 0 is encountered
  2. For each subarray use one pass to check for negative numbers - even or odd number of negative numbers and first and last position of those negative numbers
  3. If there are an even number of negative numbers then all the numbers in the subarray can be multiplied
  4. If there are odd number of negative numbers then calculate two products - one that contains the first negative number and the other that contains the last negative number. Choose the maximum of those two.
  5. Finally, return the maximum of 0 and the products of all the subarrays.

I tested my solution against some of the most voted ones, listed below:

  1. Python-solutions-with-different-space-costs-(O(2*n)-O(1))
  2. Python-Simple-Solution-Explained-(video+code)-(91-faster)
  3. Python-Easily-understandable-Solution-using-kadanes-algorithm
  4. Python-solution-with-detailed-explanation

Out of three runs, my speed varied between 86.5% to 98.91% while memory usage varied between 36.87% to 85.49%. On the fastest run, my speed was better than 98.91% of the solutions submitted and memory usage on that run was 63.88% better than solutions submitted. In contrast, solution 4 - `Python-solution-with-detailed-explanation` - in the list above had a memory utilisation better than 95.37% solutions but faster than only 69.01%. Solution 2 - `Python-Simple-Solution-Explained-(video+code)-(91-faster)` - was as fast, faster than 98.91% of the solutions, but had a memory utilisation better than a meager 14.54% of the solutions.

However, my solution remains quite unreadable; maybe there is some recursive version that I have not thought of. However, I found solution 2 to be most elegant with a helpful explanation video too.

Here is my code in its entirety:

No alt text provided for this image

要查看或添加评论,请登录

Ratnadeep Bhattacharya的更多文章

  • C/C++ vs Rust vs Go vs Python: Can you really compare them?

    C/C++ vs Rust vs Go vs Python: Can you really compare them?

    Let me start with a slight background about myself. I am an IT systems engineer who started writing code in 2014 and…

  • Safe Global State in Rust: Raw Pointers aboard!

    Safe Global State in Rust: Raw Pointers aboard!

    It is pretty common in almost all large projects, at least the ones I have seen, to use a global state of usually…

  • Container Network Interface (CNI) - A Summary

    Container Network Interface (CNI) - A Summary

    This is a topic that has been turning over in the back of my mind for a while. In short, sometime last year, I…

  • Using round robin costs you money!

    Using round robin costs you money!

    A lot of load balancers use the round robin (RR) policy as default. And most users leave it as such.

    4 条评论
  • How I made a life-altering decision at 34

    How I made a life-altering decision at 34

    I am currently a 34-year-old married man with two kids, an IT system administrator by profession. And I am starting my…

    47 条评论
  • Our Fear Holds us BACK!

    Our Fear Holds us BACK!

    It was 2002, when I passed out of high school and was looking to get started in a career. My father, a graduate of the…

  • Machine Learning for IT Admins

    Machine Learning for IT Admins

    A few years back, I took Andrew Ng's class on machine learning from Coursera. This class got me enthused about the…

    1 条评论
  • Why does it make sense to be a part of the Open Source community

    Why does it make sense to be a part of the Open Source community

    I came across the open source movement in 2006 when I truly got introduced to the Linux operating system. As far as I…

    1 条评论
  • System Engineer vs System Administrator

    System Engineer vs System Administrator

    Sometime back I had come across a discussion on LinkedIn on the distinction between System Engineers (core engineering)…

    16 条评论
  • VXLAN by VMware - A complete compilation

    VXLAN by VMware - A complete compilation

    What was the problem anyway? This should be the first place to start and I will not say much here other than to provide…

社区洞察

其他会员也浏览了