|
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. |
3 | 3 |
|
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. |
8 | 8 |
|
9 | | -#Example 2: |
10 | | -#Input: nums = [1] |
11 | | -#Output: 1 |
| 9 | +# Example 2: |
| 10 | +# Input: nums = [1] |
| 11 | +# Output: 1 |
12 | 12 |
|
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 |
16 | 16 |
|
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 |
20 | 20 |
|
| 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 |
21 | 31 |
|
| 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] |
22 | 37 |
|
| 38 | + # return first number if array length is 1 |
| 39 | + return max_sum if nums.length == 1 |
23 | 40 |
|
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 |
33 | 43 |
|
| 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 |
34 | 47 |
|
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 |
56 | 54 | 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