当前位置:网站首页>贪心——53. 最大子数组和
贪心——53. 最大子数组和
2022-07-28 03:21:00 【向着百万年薪努力的小赵】
1 题目描述
- 最大子数组和
给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
子数组 是数组中的一个连续部分。
2 题目示例
示例 1:
输入:nums = [-2,1,-3,4,-1,2,1,-5,4]
输出:6
解释:连续子数组 [4,-1,2,1] 的和最大,为 6 。
示例 2:
输入:nums = [1]
输出:1
示例 3:
输入:nums = [5,4,-1,7,8]
输出:23
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/maximum-subarray
3 题目提示
1 <= nums.length <= 105
-104 <= nums[i] <= 104
4 思路
贪心贪的是哪里呢?
如果-2 1在一起,计算起点的时候,一定是从1开始计算,因为负数只会拉低总和,这就是贪心贪的地方!
局部最优:当前“连续和"为负数的时候立刻放弃,从下一个元素重新计算"连续和",因为负数加上下一个元素“连续和"只会越来越小。
全局最优:选取最大"连续和”
局部最优的情况下,并记录最大的“连续和”,可以推出全局最优。
从代码角度上来讲:遍历nums,从头开始用count累积,如果count一旦加上nums[i]变为负数,那么就应该从nums[i+1]开始从0累积count了,因为已经变为负数的count,只会拖累总和。
这相当于是暴力解法中的不断调整最大子序和区间的起始位置。
那有同学问了,区间终止位置不用调整么?如何才能得到最大"连续和"呢?
区间的终止位置,其实就是如果count取到最大值了,及时记录下来了。例如如下代码:
if (count > result) result = count;
这样相当于是用result记录最大子序和区间和(变相的算是调整了终止位置)。
红色的起始位置就是贪心每次取count为正数的时候,开始一个区间的统计。
时间复杂度:O(n)·空间复杂度:O(1)
当然题目没有说如果数组为空,应该返回什么,所以数组为空的话返回啥都可以了。
不少同学认为如果输入用例都是-1,或者都是负数,这个贪心算法跑出来的结果是0,这是又一次证明脑洞模拟不靠谱的经典案例,建议大家把代码运行一下试一试,就知道了,也会理解为什么result 要初始化为最小负数了。
5 我的答案
class Solution {
public int maxSubArray(int[] nums) {
if (nums.length == 1){
return nums[0];
}
int sum = Integer.MIN_VALUE;
int count = 0;
for (int i = 0; i < nums.length; i++){
count += nums[i];
sum = Math.max(sum, count); // 取区间累计的最大值(相当于不断确定最大子序终止位置)
if (count <= 0){
count = 0; // 相当于重置最大子序起始位置,因为遇到负数一定是拉低总和
}
}
return sum;
}
}
边栏推荐
- What is a virtual function?
- Integrate SSM to realize search of addition, deletion, modification and query
- 20220727 use the Bluetooth module hc-05 of Huicheng technology to pair mobile phones for Bluetooth serial port demonstration
- [5g NR] RRC reject analysis
- 12月份PMP考试首次采用新考纲,该怎么学?
- Defect detection of BP SVM system design of leaf defect detection
- ASEMI整流桥GBPC5010,GBPC5010参数,GBPC5010大小
- 695. 岛屿的最大面积
- 695. Maximum area of the island
- Redis memory recycling
猜你喜欢

每周推荐短视频:如何正确理解“精益”这个词?

沃尔沃:深入人心的“安全感” 究竟靠的是什么?

收藏|0 基础开源数据可视化平台 FlyFish 大屏开发指南

When a dialog box pops up, the following form is not available

Softek Barcode Reader 9.1.5

如何解决mysql深分页问题

单调栈——42. 接雨水——面大厂必须会的困难题

Win11怎么显示固定应用?

如何一键进行重装Win11系统
![[download file] uniapp develops small programs, downloads files and saves them locally](/img/0f/4963178e44f58ad6e3097844bb6ffd.png)
[download file] uniapp develops small programs, downloads files and saves them locally
随机推荐
Digital economy has become the biggest attraction
bp svm的缺陷检测 树叶缺陷 叶片缺陷检测的系统设计
Acid characteristics of MySQL transactions and example analysis of concurrency problems
20220726 at command test of Bluetooth module hc-05 of Huicheng Technology
GNU General Public License v2.0 GNU General Public License
「运维有小邓」网络设备监控
C # set TextBox control not editable
Defect detection of BP SVM system design of leaf defect detection
A treasure simulates login and reduces the method of secondary verification
STM32 RT thread virtual file system mount operation
Unity背包系统
D2DEngine食用教程(4)———绘制文本
golang 获取循环嵌套结构的tag
What are the fragments of MySQL
max_ pool2d(): argument ‘input‘ (position 1) must be Tensor, not NoneType
What is a virtual function?
Unity simply implements the dialog function
什么是虚函数?
过亿资产地址被拉入黑名单?Tether地址冻结功能该怎么用?
203. Remove linked list elements