Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
zz-cpp
algorithm-journey
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
algorithm-journey
/
src
/
class070
/
Code01_MaximumSubarray.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
72 lines (64 loc) · 1.87 KB
main
Breadcrumbs
algorithm-journey
/
src
/
class070
/
Code01_MaximumSubarray.java
Copy path
Top
File metadata and controls
Code
Blame
72 lines (64 loc) · 1.87 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package
class070
;
// 子数组最大累加和
// 给你一个整数数组 nums
// 返回非空子数组的最大累加和
// 测试链接 : https://leetcode.cn/problems/maximum-subarray/
public
class
Code01_MaximumSubarray
{
// 动态规划
public
static
int
maxSubArray1
(
int
[]
nums
) {
int
n
=
nums
.
length
;
// dp[i] : 子数组必须以i位置的数做结尾,往左能延伸出来的最大累加和
int
[]
dp
=
new
int
[
n
];
dp
[
0
] =
nums
[
0
];
int
ans
=
nums
[
0
];
for
(
int
i
=
1
;
i
<
n
;
i
++) {
dp
[
i
] =
Math
.
max
(
nums
[
i
],
dp
[
i
-
1
] +
nums
[
i
]);
ans
=
Math
.
max
(
ans
,
dp
[
i
]);
}
return
ans
;
}
// 空间压缩
public
static
int
maxSubArray2
(
int
[]
nums
) {
int
n
=
nums
.
length
;
int
ans
=
nums
[
0
];
for
(
int
i
=
1
,
pre
=
nums
[
0
];
i
<
n
;
i
++) {
pre
=
Math
.
max
(
nums
[
i
],
pre
+
nums
[
i
]);
ans
=
Math
.
max
(
ans
,
pre
);
}
return
ans
;
}
// 如下代码为附加问题的实现
// 子数组中找到拥有最大累加和的子数组
// 并返回如下三个信息:
// 1) 最大累加和子数组的开头left
// 2) 最大累加和子数组的结尾right
// 3) 最大累加和子数组的累加和sum
// 如果不止一个子数组拥有最大累加和,那么找到哪一个都可以
public
static
int
left
;
public
static
int
right
;
public
static
int
sum
;
// 找到拥有最大累加和的子数组
// 更新好全局变量left、right、sum
// 上游调用函数可以直接使用这三个变量
// 相当于返回了三个值
public
static
void
extra
(
int
[]
nums
) {
sum
=
Integer
.
MIN_VALUE
;
for
(
int
l
=
0
,
r
=
0
,
pre
=
Integer
.
MIN_VALUE
;
r
<
nums
.
length
;
r
++) {
if
(
pre
>=
0
) {
// 吸收前面的累加和有利可图
// 那就不换开头
pre
+=
nums
[
r
];
}
else
{
// 吸收前面的累加和已经无利可图
// 那就换开头
pre
=
nums
[
r
];
l
=
r
;
}
if
(
pre
>
sum
) {
sum
=
pre
;
left
=
l
;
right
=
r
;
}
}
}
}
You can’t perform that action at this time.