dev-resources.site
for different kinds of informations.
Kadane's Algorithm: Leetcode 53 Maximum subarray
Published at
1/10/2025
Categories
java
algorithms
leetcode
datastructures
Author
devn913
Author
7 person written this
devn913
open
Intuition
We can build the intuition based on the two-point approach.
Approach
We will start with two variables maxSum
and maxTillNow
.
The first variable stores the max sum we have attained overall in the array.
The second variable stores the value of the maximum sum attained till the current index. Since the array has a negative value, this value will fluctuate, but whenever we get
maxSum < maxTillNow
, we update themaxSum
.The final case we have to handle will be if the maximum sum till any index reaches zero, i.e.,
maxTillNow < 0
, reset themaxTillNow = 0
at the end of loop.
Complexity
-
Time complexity: O(N)
-
Space complexity: O(1)
Code
class Solution {
public int maxSubArray(int[] nums) {
int maxSum = Integer.MIN_VALUE;
int maxTillNow = 0;
for(int i =0;i<nums.length;i++){
maxTillNow+=nums[i];
maxSum = Math.max(maxTillNow,maxSum);
if(maxTillNow<0) maxTillNow = 0;
}
return maxSum;
}
}
GitHub repo for more solutions: Git
Leetcode profile: Leetcode: devn007
algorithms Article's
30 articles in total
From Bootcamp to Senior Engineer: Growing, Learning, and Feeling Green
read article
Neetcode Roadmap Part 1
read article
A técnica dos dois ponteiros
read article
2429. Minimize XOR
read article
How to learn DSA , Complete Roadmap with Resources
read article
2657. Find the Prefix Common Array of Two Arrays
read article
Wanna Know Big O Basics!
read article
Time Complexity, Big-O for Beginners
read article
I am a beginner in Python programming and I want to develop my skills.
read article
3223. Minimum Length of String After Operations
read article
LinearBoost: Faster Than XGBoost and LightGBM, Outperforming Them on F1 Score on Seven Famous Benchmark Datasets
read article
Hoof It
read article
2116. Check if a Parentheses String Can Be Valid
read article
Understanding Quick Sort in Kotlin : A Beginner's Guide
read article
External Merge Problem - Complete Guide for Gophers
read article
Greedy Algorithm With Examples
read article
From 0 to O(n): A Beginner's Guide to Big O Notation
read article
Understanding Selection Sort in Kotlin: A Beginner's Guide
read article
Entity and Relationship
read article
HackerRank’s Flipping the Matrix Problem
read article
Understanding the XOR Operator: A Powerful Tool in Computing
read article
I am currently reducing at least 22 proofs by at least 99312 steps total.
read article
Kadane's Algorithm: Leetcode 53 Maximum subarray
currently reading
Building a Production-Ready Trie Search System: A 5-Day Journey 🚀
read article
AI and Machine Learning Algorithms: Strengths, Weaknesses and Best Use Cases
read article
Best Way to Learn Data Science: A Comprehensive Guide for Aspiring Experts
read article
Symmetric data encryption with Python
read article
Disk Fragmenter
read article
Time and Space Complexity
read article
Простая задача с собеседования в Google: Merge Strings Alternately
read article
Featured ones: