Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e1e1377

Browse files
committed
solve maximum_subarray
1 parent ca740fe commit e1e1377

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

‎algs/0053.maximum_subarray.go‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package algs
2+
3+
// MaxSubArray
4+
//
5+
// Given an integer array nums, find the
6+
// subarray
7+
// with the largest sum, and return its sum.
8+
//
9+
// Example 1:
10+
//
11+
// Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
12+
// Output: 6
13+
// Explanation: The subarray [4,-1,2,1] has the largest sum 6.
14+
//
15+
// Example 2:
16+
//
17+
// Input: nums = [1]
18+
// Output: 1
19+
// Explanation: The subarray [1] has the largest sum 1.
20+
//
21+
// Example 3:
22+
//
23+
// Input: nums = [5,4,-1,7,8]
24+
// Output: 23
25+
// Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.
26+
//
27+
// Constraints:
28+
//
29+
// 1 <= nums.length <= 10^5
30+
// -10^4 <= nums[i] <= 10^4
31+
//
32+
// Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
33+
func MaxSubArray(nums []int) int {
34+
// 状态转移方程:
35+
// f(i) = max(f(i-1), nums[i])
36+
//
37+
// 初始状态:
38+
// f(0) = nums[0]
39+
40+
// dp dp 表表示 f(i-1) 的值
41+
dp := nums[0]
42+
res := dp
43+
for i := 1; i < len(nums); i++ {
44+
dp = max(nums[i], dp+nums[i])
45+
if dp > res {
46+
res = dp
47+
}
48+
}
49+
return res
50+
}

‎src/solution.rs‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,4 +679,60 @@ impl Solution {
679679

680680
return s[l..r].to_string();
681681
}
682+
683+
/// [53. maximum subarray](https://leetcode.cn/problems/maximum-subarray/description)
684+
///
685+
/// Given an integer array nums, find the
686+
/// subarray
687+
/// with the largest sum, and return its sum.
688+
///
689+
///
690+
///
691+
/// Example 1:
692+
///
693+
/// Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
694+
/// Output: 6
695+
/// Explanation: The subarray [4,-1,2,1] has the largest sum 6.
696+
///
697+
/// Example 2:
698+
///
699+
/// Input: nums = [1]
700+
/// Output: 1
701+
/// Explanation: The subarray [1] has the largest sum 1.
702+
///
703+
/// Example 3:
704+
///
705+
/// Input: nums = [5,4,-1,7,8]
706+
/// Output: 23
707+
/// Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.
708+
///
709+
///
710+
///
711+
/// Constraints:
712+
///
713+
/// 1 <= nums.length <= 10^5
714+
/// -10^4 <= nums[i] <= 10^4
715+
///
716+
///
717+
///
718+
/// Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
719+
pub fn max_sub_array(nums: Vec<i32>) -> i32 {
720+
if nums.len() == 0 {
721+
return 0;
722+
}
723+
// 状态转移方程:
724+
// f(i) = max(f(i-1), nums[i])
725+
//
726+
// 初始状态:
727+
// f(0) = nums[0]
728+
729+
// dp dp 表表示 f(i-1) 的值
730+
let mut dp = nums[0];
731+
let mut res = dp;
732+
for i in 1..nums.len() {
733+
dp = std::cmp::max(nums[i], dp + nums[i]);
734+
res = std::cmp::max(res, dp);
735+
}
736+
res
737+
}
682738
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /