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的更多相关文章

  1. [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 ...

  2. 2016huasacm暑假集训训练五 J - Max Sum

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/J 题意:求一段子的连续最大和,只要每个数都大于0 那么就会一直增加,所以只要和0 ...

  3. 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 ...

  4. HDU 1024 max sum plus

    A - Max Sum Plus Plus Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  5. hdu 1024 Max Sum Plus Plus

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  6. hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行

    测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...

  7. 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 ...

  8. hdu 1003 Max sum(简单DP)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem ...

  9. HDU 1003 Max Sum

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

随机推荐

  1. Crypto支付宝模块的安装

    沙箱环境地址:https://openhome.alipay.com/platform/appDaily.htm?tab=info 1.将Crypto放到site-packages下 2.OSErro ...

  2. net core体系-web应用程序-4asp.net core2.0 项目实战(1)-13基于OnActionExecuting全局过滤器,页面操作权限过滤控制到按钮级

    1.权限管理 权限管理的基本定义:百度百科. 基于<Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员.后台管理员同时登录>我们做过了登录认证, ...

  3. 用webpack4从零开始构建react脚手架

    用webpack4从零开始构建react脚手架 使用脚手架 git clone git@github.com:xiehaitao0229/react-wepack4-xht.git` `cd reac ...

  4. SSH项目需要的所有架包

    原地址:https://blog.csdn.net/qq_35816104/article/details/54346182   SSH框架:struts2 hibernate spring 该三大框 ...

  5. elk安装&集群配置

    ---恢复内容开始--- 这里我用以elasticsearch-5.3.2.kibana-5.3.0.logstash-5.3.0的版本为例: 1.创建elastic用户,这里elasticsearc ...

  6. C# ENUM 字符串输出功能

    public enum MeasurementType { Each, [DisplayText("Lineal Metres")] LinealMetre, [DisplayTe ...

  7. 2017-10-29—英语发音的一些技巧总结

    学习了这么多年英语还是一句口语也说不出口,大家一定像我一样有hin多的f*k想说. 在很小的时候我们就学了英语音标,知道了有前元音.中元音.后元音(很多同志多年不用应该已经把这些忘得差不多了,like ...

  8. EL表达式与标签的使用

  9. Urozero Autumn 2016. BAPC 2016

    A. Airport Logistics 根据光路最快原理以及斯涅尔定律,可以得到从定点$P$进入某条直线的最佳入射角. 求出每个端点到每条线段的最佳点,建图求最短路即可. 时间复杂度$O(n^2\l ...

  10. 在Linux下开发多语言软件(gettext解决方案)

    最近的项目出现了一个bug.项目是基于一个已有的成熟开源软件之上做修改的,新写了加解密库,用于为该成熟开源软件增添加解密功能.功能增加完成后效果都很好,可是就是中文出不来了,也就是说没办法自适应多语言 ...