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. 咸鱼入门到放弃6--jsp<一>三指令

    JSP全称是Java Server Pages 它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术.JSP这门技术的最大的特点在于,写jsp就像在写html,但它相比htm ...

  2. 蓝桥杯 全球变暖(dfs)

    标题:全球变暖 [题目描述]你有一张某海域NxN像素的照片,"."表示海洋."#"表示陆地,如下所示: 其中"上下左右"四个方向上连在一起的 ...

  3. Metasploit学习记录---Nessus安装部署

    1.Nessus介绍 nessus是目前世界上最为流行的漏洞扫描器之一.她提供完整的电脑漏洞扫描服务,并随时更新其漏洞数据库.Nessus不同于传统的漏洞扫描软件,可同时在本机或远端上遥控,进行系统的 ...

  4. python 3 爬取某小说网站小说,注释详细

    目标:每一个小说保存成一个txt文件 思路:获取每个小说地址(图一),进入后获取每章节地址(图二),然后进入获取该章节内容(图三)保存文件中.循环 效果图: 每一行都有注释,不多解释了 import ...

  5. LeetCode(21. 合并两个有序链表)

    问题描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1-& ...

  6. 移动端click事件清除

    http://blog.csdn.net/zfy865628361/article/details/49512095

  7. 【安全性测试】Android测试中的一点小发现

    在执行某个项目中的APP测试发现的两个问题,自然也是提供参考,作为经验记录下来. 一.通过apk的xml文件获取到某项目APP的账号和密码 使用eclipsel或者drozer,获得apk的xml文件 ...

  8. HTML入门3

    HTML主要工作在于编辑文本结构和文本内容,也称语义(semantics)以便能够再浏览器正确地显示,下面开始介绍在文本中添加标题,段落,强调语句,创建列表等等 基础:标题和段落 内容结构化会使得阅读 ...

  9. mobile_点透_传透_touch-action

    点透(传透) <meta name="viewport" content="width=device-width, initial-scale=1.0, user- ...

  10. laravel之数据库增删改查