[LintCode笔记了解一下]44.Minimum Subarray
这道题和max subarray很类似,我用local 和 global 的dp方式阔以解决这道
那么我们来看动态规划的四个要素分别是什么?
State:
localmin[i] 表示以当前第i个数最为结尾的最小连续子数组和。
globalmin[i] 表示以当i个数里面(可以不以第i个作为结尾)的最小连续子数组和。
Function:
localmin[i] = min(localmin[i - 1] + nums.get(i), nums.get(i));
globalmin[i] = min(globalmin[i - 1], localmin[i]);
initialize:
globalmin[0] = localmin[0] = nums.get(0);
answer:
globalmin[n-1]/
优化:
由于这道题第i个状态只跟i-1的状态有关,所以这道题还可以用滚动数组
vector<int> localMin(nums.size());
vector<int> globalMin(nums.size());
localMin[]=globalMin[]=nums[];
for(int i=;i<nums.size();i++){
localMin[i]=min(localMin[i-]+nums[i],nums[i]);
globalMin[i]=min(globalMin[i-],localMin[i]);
}
return globalMin[nums.size()-];
[LintCode笔记了解一下]44.Minimum Subarray的更多相关文章
- [LintCode笔记了解一下]41.Maximum Subarray
Given an array of integers, find a contiguous subarray which has the largest sum. 首先 當題目涉及到求最大最小值時,最 ...
- Lintcode: Minimum Subarray 解题报告
Minimum Subarray 原题链接: http://lintcode.com/zh-cn/problem/minimum-subarray/# Given an array of intege ...
- Lintcode: Minimum Subarray
Given an array of integers, find the subarray with smallest sum. Return the sum of the subarray. Hav ...
- lintcode:Minimum Subarray 最小子数组
题目: 最小子数组 给定一个整数数组,找到一个具有最小和的子数组.返回其最小和. 样例 给出数组[1, -1, -2, 1],返回 -3 注意 子数组最少包含一个数字 解题: 和最大子数组 ,差不多的 ...
- LintCode笔记 - 8. 旋转字符串
这一题相对简单,但是代码质量可能不是很好,我分享一下我的做题笔记以及做题过程给各位欣赏,有什么不足望各位大佬指出来 原题目,各位小伙伴也可以试着做一下 . 旋转字符串 中文English 给定一个字符 ...
- LeetCode 笔记系列16.3 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]
题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...
- LeetCode 笔记系列16.2 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]
题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...
- LeetCode 笔记系列16.1 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
- LintCode笔记 - 145.大小写转换 - 极简之道 - 最短代码
这道题目一眼就能看出是送分题,当然在这里也不谈高难度的实现逻辑,肯定有同学会想直接用自带函数实现不就可以了吗? 对的,就是这么简单,然而今天的重点是如何把代码简写到最短. 本文章将带你把代码长度从 一 ...
随机推荐
- open the flashback
1.打开flashback: 关闭数据库 启动到mount方式 SQL>startup mount; 如果归档没有打开,打开归档[因为flashback依赖Media recovery,所以在打 ...
- oracle用户解锁
创建一个概要文件 create profile frank_profile limit SESSIONS_PER_USER 5 IDLE_TIME 2 FAILED_LOGIN_ ...
- Cinder Backup备份
cinder 备份提供了三种驱动服务: Ceph,TSM,Swift 其中默认备份驱动服务为swift cinder 驱动服务的配置在cinder.conf文件中 backup_driver=cind ...
- Tkinter Anchors(锚)
Python GUI - Tkinter Anchors: 锚是用来定义文本的相对位置参考点 锚是用来定义文本的相对位置参考点. 这里是锚属性可以使用的常数列表. NW N NE W CENTER ...
- day9-IO心得
Gevent协程 Select\Poll\Epoll异步IO与事件驱动 Python连接Mysql数据库操作 RabbitMQ队列 Redis\Memcached缓存 Paramiko SSH Tws ...
- Proof for Floyd-Warshall's Shortest Path Derivation Algorithm Also Demonstrates the Hierarchical Path Construction Process
(THIS BLOG WAS ORIGINALLY WRTITTEN IN CHINESE WITH LINK: http://www.cnblogs.com/waytofall/p/3732920. ...
- JNI 里使用STL
JNI里的c或者c++ 调用stl 的时候,比如引入map头文件: #include <map> 在cygwin使用NDK编译的时候,会提示: fatal error:map: No su ...
- iOS/iPhone学习系列、代码教程
part 1--入门: 1. xcode 版本下载 以及 iphone sdk 下载: 最新版本在此下载: http://developer.apple.com/devcenter/ios/index ...
- Ubuntu 15.04 开机无法进入图形界面,自动进入emergency mode解决方法
原因:关机的时候没有正常退出,直接强制关机了... 解决: 就是在那个GIVE root password for maintenance (or type control-D to continue ...
- spring 提供的属性值拷贝工具类
当需要把一个原生的类中属性拷贝到扩展类中时,使用以下类很方便: