Skip to content

Commit aecd373

Browse files
authored
add output for max_sub_array algorithm
1 parent b7d623a commit aecd373

File tree

1 file changed

+55
-45
lines changed

1 file changed

+55
-45
lines changed
Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,66 @@
1-
#Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
2-
#A subarray is a contiguous part of an array.
1+
# Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
2+
# A subarray is a contiguous part of an array.
33

4-
#Example 1:
5-
#Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
6-
#Output: 6
7-
#Explanation: [4,-1,2,1] has the largest sum = 6.
4+
# Example 1:
5+
# Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
6+
# Output: 6
7+
# Explanation: [4,-1,2,1] has the largest sum = 6.
88

9-
#Example 2:
10-
#Input: nums = [1]
11-
#Output: 1
9+
# Example 2:
10+
# Input: nums = [1]
11+
# Output: 1
1212

13-
#Example 3:
14-
#Input: nums = [5,4,-1,7,8]
15-
#Output: 23
13+
# Example 3:
14+
# Input: nums = [5,4,-1,7,8]
15+
# Output: 23
1616

17-
#Constraints:
18-
#1 <= nums.length <= 3 * 104
19-
#-105 <= nums[i] <= 105
17+
# Constraints:
18+
# 1 <= nums.length <= 3 * 104
19+
# -105 <= nums[i] <= 105
2020

21+
# Dynamic Programming Approach (Kadane's Algorithm) - O(n) Time / O(1) Space
22+
#
23+
# Init max_sum as first element
24+
# Return first element if the array length is 1
25+
# Init current_sum as 0
26+
# Iterate through the array:
27+
# if current_sum < 0, then reset it to 0 (to eliminate any negative prefixes)
28+
# current_sum += num
29+
# max_sum = current_sum if current_sum is greater than max_sum
30+
# Return max_sum
2131

32+
# @param {Integer[]} nums
33+
# @return {Integer}
34+
def max_sub_array(nums)
35+
# initialize max sum to first number
36+
max_sum = nums[0]
2237

38+
# return first number if array length is 1
39+
return max_sum if nums.length == 1
2340

24-
#Dynamic Programming Approach (Kadane's Algorithm) - O(n) Time / O(1) Space
25-
#Init max_sum as first element
26-
#Return first element if the array length is 1
27-
#Init current_sum as 0
28-
#Iterate through the array:
29-
#if current_sum < 0, then reset it to 0 (to eliminate any negative prefixes)
30-
#current_sum += num
31-
#max_sum = current_sum if current_sum is greater than max_sum
32-
#Return max_sum
41+
# init current sum to 0
42+
current_sum = 0
3343

44+
# iterate through array, reset current_sum to 0 if it ever goes below 0, track max_sum with highest current_sum
45+
nums.each do |num|
46+
current_sum = 0 if current_sum < 0
3447

35-
# @param {Integer[]} nums
36-
# @return {Integer}
37-
def max_sub_array(nums)
38-
#initialize max sum to first number
39-
max_sum = nums[0]
40-
41-
#return first number if array length is 1
42-
return max_sum if nums.length == 1
43-
44-
#init current sum to 0
45-
current_sum = 0
46-
47-
#iterate through array, reset current_sum to 0 if it ever goes below 0, track max_sum with highest current_sum
48-
nums.each do |num|
49-
current_sum = 0 if current_sum < 0
50-
current_sum += num
51-
max_sum = [max_sum, current_sum].max
52-
end
53-
54-
#return answer
55-
max_sum
48+
current_sum += num
49+
50+
max_sum = [max_sum, current_sum].max
51+
end
52+
53+
max_sum
5654
end
55+
56+
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
57+
print max_sub_array(nums)
58+
# Output: 6
59+
60+
nums = [1]
61+
print max_sub_array(nums)
62+
# Output: 1
63+
64+
nums = [5, 4, -1, 7, 8]
65+
print max_sub_array(nums)
66+
# Output: 23

0 commit comments

Comments
 (0)