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 ...
随机推荐
- scrapy_redis 相关: 查看保存的数据
0.参考资料 https://redis.io/topics/data-types-intro An introduction to Redis data types and abstractions ...
- sklearn数据库-【老鱼学sklearn】
在做机器学习时需要有数据进行训练,幸好sklearn提供了很多已经标注好的数据集供我们进行训练. 本节就来看看sklearn提供了哪些可供训练的数据集. 这些数据位于datasets中,网址为:htt ...
- 全文搜索引擎Elasticsearch入门实践
全文搜索引擎Elasticsearch入门实践 感谢阮一峰的网络日志全文搜索引擎 Elasticsearch 入门教程 安装 首先需要依赖Java环境.Elasticsearch官网https://w ...
- pc端网页,移动端网页(andorid、ios)离开页面做一个时间存储
如图所示:在一个页面中做了一个倒计时,然后用户想离开页面做其他事情,需求是离开页面之后把时间保存,下一次进来继续的时候时间还是上次离开的时间 第一次我用的事件是: // window.onbefor ...
- vs2019离线安装包下载
https://docs.microsoft.com/en-us/visualstudio/install/use-command-line-parameters-to-install-visual- ...
- CSS(六)
CSS权重 CSS权重指的是样式的优先级,有两条或多条样式作用于一个元素,权重高的那条样式对元素起作用,权重相同的,后写的样式会覆盖前面写的样式. 权重的等级 可以把样式的应用方式分为几个等级,按照等 ...
- C#中异步调用示例与详解
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServi ...
- vue_eHungry 饿了么
eHungry 仿饿了么 git 操作 git checkout -b dev // 创建新分支 dev git push origin dev // 代码推送到 dev ...
- 16进制转化8进制---map
#include "stdio.h" #include "string.h" #include "string" #include &quo ...
- Go语言基础之time包
Go语言基础之time包 时间和日期是我们编程中经常会用到的,本文主要介绍了Go语言内置的time包的基本用法. Go语言中导入包 Go语言中使用import关键字导入包,包的名字使用双引号(”)包裹 ...