Given an integer array nums, find the subarray with the largest sum, and return its sum.
The subarray [4,-1,2,1] has the largest sum 6.
The subarray [1] has the largest sum 1.
The subarray [5,4,-1,7,8] has the largest sum 23.
1 <= nums.length <= 10^5-10^4 <= nums[i] <= 10^4No developer has claimed the Python throne for LeetCode #53 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Greedy Algorithm (please see solution 2), yet here we will use another way.nums[i]:
previous sum is negative, we can discard previous sum;previous sum is positive, we can add previous sum to the current sum.dynamic programming problem.class Solution:
def maxSubArray(self, nums: List[int]) -> int:
dp = nums.copy()
for i in range(1, len(dp)):
dp[i] = max(nums[i], dp[i - 1] + nums[i])
return max(dp)
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
result = -float('inf')
pre_sum = 0
for num in nums:
pre_sum = max(pre_sum + num, num)
result = max(result, pre_sum)
return result
No developer has claimed the Java throne for LeetCode #53 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Greedy Algorithm (please see solution 2), yet here we will use another way.nums[i]:
previous sum is negative, we can discard previous sum;previous sum is positive, we can add previous sum to the current sum.dynamic programming problem.class Solution {
public int maxSubArray(int[] nums) {
var dp = nums.clone();
for (var i = 1; i < dp.length; i++) {
dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]);
}
return IntStream.of(dp).max().getAsInt(); // if you want to beat 99%, refer to C# soluiton's comment
}
}
class Solution {
public int maxSubArray(int[] nums) {
int result = Integer.MIN_VALUE;
int preSum = 0;
for (int num : nums) {
preSum = Math.max(preSum + num, num);
result = Math.max(result, preSum);
}
return result;
}
}
No developer has claimed the Javascript throne for LeetCode #53 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Greedy Algorithm (please see solution 2), yet here we will use another way.nums[i]:
previous sum is negative, we can discard previous sum;previous sum is positive, we can add previous sum to the current sum.dynamic programming problem.var maxSubArray = function (nums) {
const dp = [...nums]
for (let i = 1; i < dp.length; i++) {
dp[i] = Math.max(nums[i], dp[i - 1] + nums[i])
}
return Math.max(...dp)
};
/**
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function(nums) {
let result = -Infinity;
let preSum = 0;
for (const num of nums) {
preSum = Math.max(preSum + num, num);
result = Math.max(result, preSum);
}
return result;
};
No developer has claimed the Go throne for LeetCode #53 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Greedy Algorithm (please see solution 2), yet here we will use another way.nums[i]:
previous sum is negative, we can discard previous sum;previous sum is positive, we can add previous sum to the current sum.dynamic programming problem.func maxSubArray(nums []int) int {
dp := slices.Clone(nums)
for i := 1; i < len(nums); i++ {
dp[i] = max(nums[i], dp[i - 1] + nums[i])
}
return slices.Max(dp)
}
func maxSubArray(nums []int) int {
result := math.MinInt
preSum := 0
for _, num := range nums {
preSum = max(preSum + num, num)
if preSum > result {
result = preSum
}
}
return result
}
No developer has claimed the Ruby throne for LeetCode #53 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Greedy Algorithm (please see solution 2), yet here we will use another way.nums[i]:
previous sum is negative, we can discard previous sum;previous sum is positive, we can add previous sum to the current sum.dynamic programming problem.def max_sub_array(nums)
dp = nums.clone
(1...dp.size).each do |i|
dp[i] = [ nums[i], dp[i - 1] + nums[i] ].max
end
dp.max
end
# @param {Integer[]} nums
# @return {Integer}
def max_sub_array(nums)
result = -Float::INFINITY
pre_sum = 0
nums.each do |num|
pre_sum = [pre_sum + num, num].max
result = [result, pre_sum].max
end
result
end
No developer has claimed the C# throne for LeetCode #53 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Greedy Algorithm (please see solution 2), yet here we will use another way.nums[i]:
previous sum is negative, we can discard previous sum;previous sum is positive, we can add previous sum to the current sum.dynamic programming problem.public class Solution
{
public int MaxSubArray(int[] nums)
{
var dp = (int[])nums.Clone();
for (var i = 1; i < dp.Length; i++)
{
dp[i] = Math.Max(nums[i], dp[i - 1] + nums[i]);
}
return dp.Max(); // if you want to beat 99%, you can use a variable to collect the maximum value: `if (dp[i] > result) result = dp[i];`
}
}
public class Solution
{
public int MaxSubArray(int[] nums)
{
int result = int.MinValue;
int preSum = 0;
foreach (int num in nums)
{
preSum = Math.Max(preSum + num, num);
result = Math.Max(result, preSum);
}
return result;
}
}
No developer has claimed the C++ throne for LeetCode #53 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Greedy Algorithm (please see solution 2), yet here we will use another way.nums[i]:
previous sum is negative, we can discard previous sum;previous sum is positive, we can add previous sum to the current sum.dynamic programming problem.class Solution {
public:
int maxSubArray(vector<int>& nums) {
auto dp = nums;
for (auto i = 1; i < dp.size(); i++) {
dp[i] = max(nums[i], dp[i - 1] + nums[i]);
}
return *max_element(dp.begin(), dp.end());
}
};
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int result = INT_MIN;
int pre_sum = 0;
for (int num : nums) {
pre_sum = max(pre_sum + num, num);
result = max(result, pre_sum);
}
return result;
}
};
No developer has claimed the Rust throne for LeetCode #53 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Swift throne for LeetCode #53 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Kotlin throne for LeetCode #53 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.