【LeetCode】053. Maximum Subarray
题目:
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [-2,1,-3,4,-1,2,1,-5,4]
,
the contiguous subarray [4,-1,2,1]
has the largest sum = 6
.
题解:
遍历所有组合,更新最大和。
Solution 1 (TLE)
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int n = nums.size(), result = nums[];
for(int i=; i<n; ++i) {
int tmp = nums[i];
result = max(tmp,result);
for(int j=i+; j<n; ++j) {
tmp += nums[j];
result = max(tmp,result);
}
}
return result;
}
};
可以看做动态规划的简略版,见Solution 5
Solution 2 ()
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int n = nums.size(), result = nums[], tmp = ;
for(int i=; i<n; ++i) {
tmp = max(tmp + nums[i], nums[i]);
result = max(result, tmp);
}
return result;
}
};
贪心算法:The idea is to find the largest difference between the sums when you summing up the array from left to right. The largest difference corresponds to the sub-array with largest sum
Solution 3 ()
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int sum = , minsum = , result = nums[], n = nums.size();
for(int i = ; i < n; i++) {
sum += nums[i];
if(sum - minsum > res) result = sum - minsum;
if(sum < minsum) minsum = sum;
}
return result;
}
};
分治法:
Solution 4 ()
DP算法:维护一个一维数组。
Solution 5 ()
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int n = nums.size();
vector<int> dp(n,);//dp[i] means the maximum subarray ending with nums[i];
dp[] = nums[];
int max = dp[]; for(int i = ; i < n; i++){
dp[i] = nums[i] + (dp[i - ] > ? dp[i - ] : );
max = Math.max(max, dp[i]);
}
return max;
}
};
【LeetCode】053. Maximum Subarray的更多相关文章
- 【LeetCode】53. Maximum Subarray (2 solutions)
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 【leetcode】1186. Maximum Subarray Sum with One Deletion
题目如下: Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elemen ...
- 【LeetCode】53. Maximum Subarray 最大子序和 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 动态规划 日期 题目地址: https:/ ...
- 【Leetcode】53. Maximum Subarray
题目地址: https://leetcode.com/problems/maximum-subarray/description/ 题目描述: 经典的求最大连续子数组之和. 解法: 遍历这个vecto ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- 【leetcode】998. Maximum Binary Tree II
题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【Leetcode】164. Maximum Gap 【基数排序】
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
随机推荐
- java nio读取和写入文件
读取 package com.test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputS ...
- web翻译——插件
很多时候,可能我们web项目中需要的只是机械式的翻译,并不需要什么利用xml或者js json等等实现逼真翻译,那样工作量太大.这时候可能你就需要这几款小工具来帮助你.当然,如果 对翻译或者你的项目外 ...
- Swift实战(一): 剪子包袱锤ios应用
来自十奶的大作业教学视频. http://www.swiftv.cn/course/ic2tqzob 主要了解了MVC模型. 首先是View,通过设计mainstoryboard构建UI界面,主要靠拖 ...
- testVC.modalPresentationStyle = UIModalPresentationFormSheet; 更改 VC大小
本文转载至 http://www.cocoachina.com/bbs/simple/?t31199.html TestViewController *testVC = [[TestViewContr ...
- Java 学习 day06
01-面向对象(Static关键字) package myFirstCode; /* 静态:static. 用法:是一个修饰符,用于修饰成员(成员变量,成员函数) 当成员被静态修饰后,就多了一个调用方 ...
- 八大排序的python实现
以下是八大排序的python实现,供以后参考,日后扩展 一.插入排序 #-*- coding:utf-8 -*- ''' 描述 插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一 ...
- Linuxt图形界面安装
通常安装图形界面需要安装Xorg.桌面环境(Gnome.KDE.xfce等).字体等.你的Linux是什么版本?具体要看你的linux来决定用什么命令来安装. #yum grouplist 自己看看列 ...
- 【题解】Fence(单调队列)
[题解]Fence(单调队列) POJ - 1821 题目大意 有\(k\)个粉刷匠,每个粉刷匠一定要粉刷某个位置\(S_i\),一个粉刷匠可以粉刷至多\(l_i\)个位置(必须连续\(l_i\)互不 ...
- 【题解】kth异或和/魔改版线性基
[题解]魔改版线性基 魔改版线性基解决此类问题. 联系线性空间的性质,我们直接可以构造出这样的基: \[ 100000 \\ 010000 \\ 000010 \\ 000001 \] 使得每个基的最 ...
- 我的Android进阶之旅------>Android如何去除GridView的按下或点击选中后的背景效果
今天用GridView做了一个界面,自己自定好了一个组件,并且设置好了点击和不点击组件时候的效果,但是运行的时候发现在我定义好的背景下面还有一层不知道哪儿来的背景,严重影响了我自定义的组件的效果. 后 ...