Skip to content

Commit 5ad402d

Browse files
committed
Adding Simple Solution for Problem - 53 - Maximum SubArray
1 parent 2754de5 commit 5ad402d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Maximum_SubArray/SimpleSolution.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
##==================================
2+
## Leetcode
3+
## Student: Vandit Jyotindra Gajjar
4+
## Year: 2020
5+
## Problem: 49
6+
## Problem Name: Group Anagrams
7+
##===================================
8+
#Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
9+
#
10+
#Example:
11+
#
12+
#Input: [-2,1,-3,4,-1,2,1,-5,4],
13+
#Output: 6
14+
#Explanation: [4,-1,2,1] has the largest sum = 6.
15+
class Solution:
16+
def maxSubArray(self, nums):
17+
sum = max_sum = nums[0] #Initialize sum and max_sum equals to 0th index value from nums
18+
for i in nums[1:]: #Loop through nums start index from 1
19+
sum = max(i, sum + i) #Update sum by finding max of i and sum + i.
20+
max_sum = max(max_sum, sum) #Update max_sum by finding max of max_sum and sum
21+
return max_sum #We'll return max_sum at the end.

0 commit comments

Comments
 (0)