🤖
Data Structures and Algorithms
  • CS Theory And Problems
  • Big O
    • Big O Notation
  • Algorithms
    • Binary Search
    • Breadth First Search (BFS)
    • Depth First Search (DFS)
    • Dynamic Programming
    • Kadane's Algorithm
    • Kahn's Algorithm
    • Quickselect
    • Recursion
    • Sorting Algorithms
    • Sliding Window
  • Data Structures
    • Binary Heap
    • Graph
    • Linked List
    • Trees
  • Problems
    • LeetCode
      • 1. Two Sum
      • 3. Longest Substring Without Repeating Characters
      • 5. Longest Palindromic Substring
      • 11. Container With Most Water
      • 15. 3 Sum
      • 19. Remove Nth Node From End of List
      • 20. Valid Parentheses
      • 33. Search in Rotated Sorted Array
      • 49. Group Anagrams
      • 53. Maximum Subarray
      • 55. Jump Game
      • 56. Merge Intervals
      • 76. Minimum Window Substring
      • 98. Validate Binary Search Tree
      • 105. Construct Binary Tree from Preorder and Inorder Traversal
      • 121. Best Time to Buy and Sell Stock
      • 133. Clone Graph
      • 141. Linked List Cycle
      • 152. Maximum Product Subarray
      • 153. Find Minimum in Rotated Sorted Array
      • 200. Number of Islands
      • 206. Reverse Linked List
      • 207. Course Schedule
      • 217. Contains Duplicate
      • 226. Invert Binary Tree
      • 238. Product of Array Except Self
      • 242. Valid Anagram
      • 297. Serialize and Deserialize Binary Tree
      • 347. Top K Frequent Elements
      • 417. Pacific Atlantic Water Flow
      • 424. Longest Repeating Character Replacement
      • 435. Non-overlapping Intervals
      • 647. Palindromic Substrings
Powered by GitBook
On this page
  • Description
  • Solutions
  • Kadane's Algorithm
  • My Solution
  1. Problems
  2. LeetCode

53. Maximum Subarray

Description

See description at https://leetcode.com/problems/maximum-subarray/

Solutions

Kadane's Algorithm

See Kadane's Algorithm. This is a slight modification of Kadane's algorithm in that it allows for an input array of all negatives (example: nums = [-5, -3, -7, -2, -4]. The result in this case will be -3. The input array is also assumed to have at least one value.

class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
        current_sum = nums[0]
        high_sum = current_sum
        for i in range(1, len(nums)):
            current_sum = max(nums[i], current_sum + nums[i])
            high_sum = max(current_sum, high_sum)
            
        return high_sum

My Solution

I was on the right track to the optimal solution which is Kadane's Algorithm. I should have taken extra time to look at my nested if statements. I knew it was bad, but just submitted anyway. If I stopped and looked closer, I think I could have simplified it closer to Kadane's Algorithm. I was happy I found the O(N) solution.

class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
        current_sum = nums[0]
        high_sum = current_sum
        for i in range(1, len(nums)):
            if current_sum < 0:
                if nums[i] > 0:
                    current_sum = nums[i]
                else:
                    current_sum = max(current_sum, nums[i])
            else:
                current_sum += nums[i]
            high_sum = max(current_sum, high_sum)
            
        return high_sum
Previous49. Group AnagramsNext55. Jump Game

Last updated 3 years ago