Created
April 4, 2024 04:31
-
-
Save codertcet111/7c5690672db5e3d28dcd2faea5df913b to your computer and use it in GitHub Desktop.
Leetcode 53 Maximum Subarray
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Leetcode 53 Maximum Subarray | |
# @param {Integer[]} nums | |
# @return {Integer} | |
def max_sub_array(nums) | |
max = nums[0] | |
sum = 0 | |
nums.each_with_index do |ele, i| | |
sum += ele | |
if sum > max | |
max = sum | |
end | |
if sum < 0 | |
sum = 0 | |
end | |
end | |
max | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment