tech-interview-handbook icon indicating copy to clipboard operation
tech-interview-handbook copied to clipboard

Change difficulty of "Maximum Subarray"

Open Shri333 opened this issue 2 years ago • 2 comments

Difficulty of the "Maximum Subarray" question on LeetCode has been updated to medium.

Shri333 avatar Jul 21 '22 14:07 Shri333

I agree, I wondered if I'm stupid enough while attempting to solve it. Needs Kadane's algorithm.

Hesham-Salama avatar Aug 03 '22 17:08 Hesham-Salama

The top-voted solution on Leetcode really made sense for me. I could not grasp this question for a long time too. Think of it as a DP algorithm rather than some fancy greedy algorithm: the subproblem is the maximum sum of a subarray ending at some index i, so dp(i) = max(dp(i-1) + A[i], A[i]). Basically, if the previous sum is negative, you are better off just starting a "new" subarray. The max sum would be the max of all dp(i) for i from 0 to n - 1 (inclusive). But, this question is no way "easy" by any means.

Shri333 avatar Aug 03 '22 17:08 Shri333

Closed via f062cb5

yangshun avatar Sep 11 '22 02:09 yangshun