FB面经prepare: 3 Window Max Sum
Given a num array, find a window of size k, that has a maximum sum of the k entries.
follow-up: Find three non-overlap windows of size k, that together has a maximum sum of the 3k entries, time complexity O(n^2)
Follow Up:
这个其实可以优化到O(n)时间。
建从左端到每个下标的最大window数组,再建从右端到每个下标的最大window数组。
再从左往右走一遍所有的size k window,将其和与上一步建的两个数组一起加起来。遍历一遍取最大值即可。
package fbOnsite;
public class ThreeWindow {
public static int find(int[] arr, int k) {
int res = Integer.MIN_VALUE;
int len = arr.length;
int[] left = new int[len];
int lsum = 0;
for (int i=0; i<len; i++) {
lsum += arr[i];
if (i >= k) lsum -= arr[i-k];
if (i >= k-1) left[i] = Math.max(lsum, i>=1? left[i-1] : 0);//find the window end at i with max sum
}
int[] right = new int[len];
int rsum = 0;
for (int j=len-1; j>=0; j--) {
rsum += arr[j];
if (j < len-k) rsum -= arr[j+k];
if (j <= len-k) right[j] = Math.max(rsum, j<len-1? right[j+1] : 0);
}
int midSum = 0;
for (int t=k; t<=len-k-1; t++) {
midSum += arr[t];
if (t >= k + k) midSum -= arr[t-k];
if (t >= k + k - 1) { // for each size k window in the middle with sum as midSum
//find midSum + left[t-k] + right[t+1] that gives the max
res = Math.max(res, midSum + left[t-k] + right[t+1]);
}
}
return res;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = new int[]{2,1,1,-1,3,6,-2,-4,1,2,-1,2,1,-1,2};
int[] arr1 = new int[]{1,2,2,-1,1,2,1,3,5};
int res = find(arr, 3);
System.out.println(res);
}
}
FB面经prepare: 3 Window Max Sum的更多相关文章
- [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
- 2016huasacm暑假集训训练五 J - Max Sum
题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/J 题意:求一段子的连续最大和,只要每个数都大于0 那么就会一直增加,所以只要和0 ...
- Max Sum
Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub ...
- HDU 1024 max sum plus
A - Max Sum Plus Plus Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- hdu 1024 Max Sum Plus Plus
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行
测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...
- Max Sum Plus Plus——A
A. Max Sum Plus Plus Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To ...
- hdu 1003 Max sum(简单DP)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem ...
- HDU 1003 Max Sum
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
随机推荐
- Linux安装Tomcat-Nginx-FastDFS-Redis-Solr-集群——【第六集之基本命令使用】
学习命令的方法:linux中所有操作都是命令操作,可想而知命令有多少,更严重的是每个命令有很多参数,记命令容易,记参数就难了,所以建议: 自己准备一个博客,把通常用到的命令及其功能记载下来,用到的时候 ...
- DDoS攻击与防御(4)
在发生DDoS攻击的情况下,可以通过一些缓解技术来减少攻击对自身业务和服务的影响,从而在一定程度上保障业务正常运行.缓解DDoS攻击的主要方法是对网络流量先进行稀释再进行清洗. 1.攻击流量的稀释 1 ...
- [C#] 動的にアセンブリをロードする
アプリケーション ドメインにおいて起動時に読み込まれない別のアセンブリ (.dll や .exe) を読み込む場合.System.Reflection.Assemby クラスの Load メソッドを使 ...
- Auth模块、Forms组件
Auth模块 auth模块是Django自带的用户认证模块: 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能,这 ...
- 2017-2018 Northwestern European Regional Contest (NWERC 2017)
A. Ascending Photo 贪心增广. #include<bits/stdc++.h> using namespace std; const int MAXN = 1000000 ...
- 读取gzmt.csv文件,计算均值及概率
问题: 读取gzmt.csv文件所有数据,选取收盘价格(倒数第二列),计算20天均值,权重取成交量(选做:时间权重为半衰期为15天):将该均值修剪为超过600的都设置为1000,并打印出该均值超过55 ...
- (90)Wangdao.com第二十三天_JavaScript CSS 操作
CSS 操作 通过 JavaScript 操作 CSS HTML 元素的 style 属性 <div style="background-color:red; border:1px s ...
- oracle根据身份证号码 计算年龄、性别
一.Oracle根据身份证判断性别: 女生身份证: 431382198103246985 男生身份证: 150921197208173492 SQL语句如下: select decode(mod ...
- PCB布局布线
1.关键芯片的物理位置,明细信号流向,防止关键信号交叉,高速线布线通畅. 2.可装配,可维修,可测试. 3.模拟电路和数字电路分区摆放. 4.疏密有序. 5.原理图应该明确主芯片周边元件的布局要求. ...
- openlayers应用原理
1.数据组织 OpenLayers通过同层(Layer)进行组织渲染,然后通过数据源设置具体的地图数据来源.因此,Layer与Source是密切相关的对应关系,缺一不可.Layer可看做渲染地图的层容 ...