lintcode:Minimum Subarray 最小子数组
题目:
给定一个整数数组,找到一个具有最小和的子数组。返回其最小和。
给出数组[1, -1, -2, 1],返回 -3
子数组最少包含一个数字
解题:
和最大子数组 ,差不多的,动态规划的还是可以继续用的
Java程序:
public class Solution {
/**
* @param nums: a list of integers
* @return: A integer indicate the sum of minimum subarray
*/
public int minSubArray(ArrayList<Integer> nums) {
// write your code
int min_ending_here = nums.get(0);
int min_so_far = nums.get(0);
for (int i=1 ;i< nums.size(); i++){
min_ending_here = Math.min(nums.get(i),nums.get(i) + min_ending_here);
min_so_far = Math.min( min_so_far, min_ending_here );
}
return min_so_far;
}
}
总耗时: 2153 ms
Python程序:
class Solution:
"""
@param nums: a list of integers
@return: A integer denote the sum of minimum subarray
"""
def minSubArray(self, nums):
# write your code here
min_ending_here = min_so_far = nums[0]
for x in nums[1:]:
min_ending_here = min(x, min_ending_here + x)
min_so_far = min(min_so_far , min_ending_here)
return min_so_far
总耗时: 648 ms
上面的空间复杂度是O(1),定义一个数组dp[i] ,表示当前i位置时候的最小子数组的值,最后再变量找出dp的最小值就是答案。
public class Solution {
/**
* @param nums: a list of integers
* @return: A integer indicate the sum of minimum subarray
*/
public int minSubArray(ArrayList<Integer> nums) {
// write your code
if( nums == null)
return 0;
int n = nums.size();
int dp[] = new int[n];
dp[0] = nums.get(0);
for(int i=1;i<n;i++){
int tmp = dp[i-1] + nums.get(i);
if(tmp > nums.get(i))
dp[i] = nums.get(i);
else
dp[i] = tmp;
}
int Min = Integer.MAX_VALUE;
for(int i =0;i< n;i++){
if(dp[i] < Min)
Min = dp[i];
}
return Min;
}
}
Java Code
总耗时: 2556 ms
lintcode:Minimum Subarray 最小子数组的更多相关文章
- 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] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- lintcode.44 最小子数组
最小子数组 描述 笔记 数据 评测 给定一个整数数组,找到一个具有最小和的子数组.返回其最小和. 注意事项 子数组最少包含一个数字 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的这个题 ...
- lintcode 中等题:和大于S的最小子数组
题目 和大于S的最小子数组 给定一个由 n 个整数组成的数组和一个正整数 s ,请找出该数组中满足其和 ≥ s 的最小长度子数组.如果无解,则返回 -1. 样例 给定数组 [2,3,1,2,4,3] ...
- Windows10系统如何安装Microsoft Visual Studio 2015及最小子数组和求解
一.Windows10系统如何安装Microsoft Visual Studio 2015. 1.首先到Visual Studio官方网站(https://www.visualstudio.com/v ...
- [LintCode] Minimum Size Subarray Sum 最小子数组和的大小
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 和大于S的最小子数组 · Minimum Size Subarray Sum
[抄题]: 给定一个由 n 个正整数组成的数组和一个正整数 s ,请找出该数组中满足其和 ≥ s 的最小长度子数组.如果无解,则返回 -1. 给定数组 [2,3,1,2,4,3] 和 s = 7, 子 ...
- LeetCode OJ:Minimum Size Subarray Sum(最小子数组的和)
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
随机推荐
- ArcGIS API for JavaScript介绍
ArcGIS API for JavaScript中的类是按照模块组织的,主要包含esri.esri/geometry.esri/renderers.esri/symbols.esri/symbols ...
- 基础学习总结(八)--HttpClient
在Android开发中,Android SDK附带了Apache的HttpClient,它是一个完善的客户端.它提供了对HTTP协议的全面支持,可以使用HttpClient的对象来执行HTTP GET ...
- 使用 PHP 验证表单数据
使用 PHP 验证表单数据 首先我们对用户所有提交的数据都通过 PHP 的 htmlspecialchars() 函数处理. 当我们使用 htmlspecialchars() 函数时,在用户尝试提交以 ...
- XE5 ANDROID通过webservice访问操作MSSQL数据库
上接XE5 ANDROID平台 调用 webservice 一.服务端 在ro里添加函数(在impl上添加阿东connection,adoquery,dataprovider) function TN ...
- [转] shell字符串操作方法,以及实例
每一种语言都有他独自的字符串操作方法,shell也一样,下面以以例子的方式,简单介绍常用方法. 1,取得字符串长度 string=abc12342341 //等号二边不要有空格 echo ${#str ...
- 每日一“酷”之heapq
作用:heapq模块实现一个适用于Python列表的最小堆排序算法 堆(heap)是一个属性数据结构,其中子节点与父节点是一种有序关系.二叉堆是一种特殊的堆,二叉堆是完全二元树(二叉树)或者是近似完全 ...
- Oracle查询出最最近一次的一条记录
需求:从一个表中查询数据,得到的数据为最新的一条记录. -------------建立测试表 --drop table TB ),dtDate date) -------------插入测试数据 ,' ...
- 【iOS】屏幕旋转,屏幕自适应方向变化
1. iOS有四个方向的旋转,为了保证自己的代码能够支持旋转,我们必须首先处理一个函数: - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInter ...
- ASP.NET Web - 回送
如果希望把更改事件立即传送给服务器,可以把AutoPostback属性设置为true.这样就会使用客户端的JavaScript把窗体数据立即提交给服务器.当然,网络通信量也会增加.使用这个功能时要小心 ...
- 爬虫组NABC
Need(需求): 我们小组的研究课题是编写一个更实用的爬虫软件,编写时会应用到学长的部分代码并在其基础上完善创新. 鉴于学长代码已经实现了基本功能,即从网站上面爬取相关的Word文档等与计算机有关的 ...