子数组最小值的总和 Sum of Subarray Minimums
2018-09-27 23:33:49
问题描述:

问题求解:
方法一、DP(MLE)
动态规划的想法应该是比较容易想到的解法了,因为非常的直观,但是本题的数据规模还是比较大的,如果直接使用动态规划,即使不MLE,也是肯定会在大规模的数据量上TLE的。
public int sumSubarrayMins(int[] A) {
int res = 0;
int mod = (int)Math.pow(10, 9) + 7;
int[][] dp = new int[A.length][A.length];
for (int i = 0; i < A.length; i++) dp[i][i] = A[i];
for (int len = 2; len <= A.length; len++) {
for (int i = 0; i <= A.length - len; i++) {
int j = i + len - 1;
dp[i][j] = Math.min(dp[i + 1][j], dp[i][j - 1]);
}
}
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A.length; j++) {
res = (res + dp[i][j]) % mod;
}
}
return res;
}
方法二、
数据量已经基本表明时间复杂度在O(nlogn)左右比较好,那么直接使用dp肯定是不会通过所有的例子的。
本题还有另外一种思路:
res = sum(A[i] * f(i))
wheref(i)is the number of subarrays,
in whichA[i]is the minimum.
难点就在于求f(i),为了求f(i)需要求left[i]和right[i]。
left[i]:A[i]左边严格大于A[i]的个数
right[i]:A[i]右边大于等于A[i]的个数
f(i) = (left[i] + 1) * (right[i] + 1),其实就是一个排列组合,左边取一个可能,那么可以从右边取right[i] + 1种来进行组合。
这里需要特别注意的一点是:首先本问题是允许重复的子数组的,这里的重复是指数字上相等,但是是不允许完全一致的区间,因此左边必须是严格大于,否则会出现重复的情况。计算left,right数组可以使用Stack在O(n)时间复杂度完成求解,最后的res计算也是线性时间,因此总的时间复杂度为O(n)。
public int sumSubarrayMins(int[] A) {
int res = 0;
int mod = (int)1e9 + 7;
int[] left = new int[A.length];
int[] right = new int[A.length];
Stack<int[]> stack = new Stack<>();
stack.push(new int[]{Integer.MIN_VALUE, -1});
for (int i = 0; i < A.length; i++) {
while (stack.peek()[0] > A[i])
stack.pop();
left[i] = i - stack.peek()[1];
stack.push(new int[]{A[i], i});
}
stack.clear();
stack.push(new int[]{Integer.MIN_VALUE, A.length});
for (int i = A.length - 1; i >= 0; i--) {
while (stack.peek()[0] >= A[i])
stack.pop();
right[i] = stack.peek()[1] - i;
stack.push(new int[]{A[i], i});
}
for (int i = 0; i < A.length; i++) {
res = (res + A[i] * left[i] * right[i]) % mod;
}
return res;
}
子数组最小值的总和 Sum of Subarray Minimums的更多相关文章
- [LeetCode] 907. Sum of Subarray Minimums 子数组最小值之和
Given an array of integers A, find the sum of min(B), where B ranges over every (contiguous) subarra ...
- [Swift]LeetCode907. 子数组的最小值之和 | Sum of Subarray Minimums
Given an array of integers A, find the sum of min(B), where B ranges over every (contiguous) subarra ...
- LeetCode 643. 子数组最大平均数 I(Maximum Average Subarray I)
643. 子数组最大平均数 I 643. Maximum Average Subarray I 题目描述 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. LeetCo ...
- 【LeetCode】643. 子数组最大平均数 I Maximum Average Subarray I (Python)
作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 目录 题目描述 题目大意 解题方法 方法一:preSum 方法二:滑动窗口 刷题心得 日期 题目地址:https://leetc ...
- [Swift]LeetCode643. 子数组最大平均数 I | Maximum Average Subarray I
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- [Swift]LeetCode930. 和相同的二元子数组 | Binary Subarrays With Sum
In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0, ...
- 907. Sum of Subarray Minimums
Given an array of integers A, find the sum of min(B), where B ranges over every (contiguous) subarra ...
- 【leetcode】907. Sum of Subarray Minimums
题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...
- leetcode907 Sum of Subarray Minimums
思路: 对于每个数字A[i],使用单调栈找到A[i]作为最小值的所有区间数量,相乘并累加结果.时间复杂度O(n). 实现: class Solution { public: int sumSubarr ...
随机推荐
- Caused by: com.rabbitmq.client.ShutdownSignalException: connection error
周五下午的时候升级了一个环境,跑了批处理sh升级脚本后,启动时报下列错误: INFO | jvm 1 | 2017/02/24 17:39:09 | java.io.IOException INFO ...
- Android之数据存储之SharedPreferences
SharedPreferences是以键值对形式存储数据,主要用于记录系统的设置,如飞行模式是否开启,声音大小的值等.//SharedPreferences方式保存到xml文件SharedPrefer ...
- get与post需要注意的几点 (转)
get与post需要注意的几点 在面试或者笔试时,经常会被问到 HTTP 方法中 get 和 post 的异同点.本文简单整理归纳了一下,以备忘. 1."get/post" VS ...
- vue.js not detected 解决办法-vue.js devtools 安装
国外网站:https://www.crx4chrome.com/ 国内网站:http://www.cnplugins.com/ http://chromecj.com/web-development/ ...
- linux下如何按行将文件切割成多个小文件
答: split -l <行数> <目标文件> <切割后的文件前缀> 举例如下: split -l 1000 jello.txt jello 将jello.txt文 ...
- vim使用跳转列表 jumps 来跟踪 (历史位置的)导航
参考: Vim使用跳转列表来跟踪你的导航,你可以通过这个列表来向前或者向后导航. 跳转列表保留所有地方的轨迹,它可以跟踪文件名.行号和列号. 查看调整列表::jumps 导航键 描述 CTRL-o 跳 ...
- BFS广度优先 vs DFS深度优先 for Binary Tree
https://www.geeksforgeeks.org/bfs-vs-dfs-binary-tree/ What are BFS and DFS for Binary Tree? A Tree i ...
- 比较好的MySQL索引原理
MySQL索引原理及慢查询优化 - 美团技术团队 https://tech.meituan.com/2014/06/30/mysql-index.html
- Bytom交易说明(账户管理模式)
比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom 该部分主 ...
- 剥开比原看代码16:比原是如何通过/list-transactions显示交易信息的
作者:freewind 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchai ...