子数组最小值的总和 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 ...
随机推荐
- spring使用@Value标签读取.properties文件的中文乱码问题的解决
最近测试某个老系统的时候,启动的时候发@Value注入的中文是乱码,文件使用GBK/UTF-8的时候均会出现乱码问题,但是spring配置文件里面注入的占位符并没有这个问题,bean文件设置了file ...
- 第一章-硬件组成及linux发展历史(1)
一.服务器与计算机的组成? 计算机组成主要有:CPU.硬盘.内存.电源.显示器.鼠标.键盘 服务器组成主要有:CPU.硬盘.内存.电源.RAID卡.远程控制卡 CPU: 即:中央处理器,是一块超大规模 ...
- PyCharm笔记之首次安装和激活
转载:http://www.cnblogs.com/Ivyli4258/p/7440147.html PyCharm是一种Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其 ...
- shell脚本之 给PNG图片添加后缀@3x
1,给png图片加上后缀@3x #!/bin/sh #root_src=$(dirname $(PWD)) #echo ${root_src} image_path=${root_src}/image ...
- ODAC(V9.5.15) 学习笔记(六)TOraSQL、TOraTable和TOraStoredProc
TOraSQL是一个SQL语句执行控件,包括PL/SQL块等,不返回数据集结果. 名称 类型 说明 ChangeCursor Boolean 在非阻塞模式下是否允许改变屏幕的光标 WaitExecut ...
- Python常用库之functools
functools 是python2.5被引人的,一些工具函数放在此包里. python2.7中 python3.6中 import functools print(dir(functools)) [ ...
- 【做题】NOWCODER142A Ternary String——数列&欧拉定理
题意:你有一个长度为\(n\),且仅由012构成的字符串.每经过一秒,这个字符串所有1后面会插入一个0,所有2后面会插入一个1,然后会删除第一个元素.求这个字符串需要多少秒变为空串,对\(10^9+7 ...
- 【调优】kafka性能调优
主要优化原理和思路 kafka是一个高吞吐量分布式消息系统,并且提供了持久化.其高性能的有两个重要特点: 利用了磁盘连续读写性能远远高于随机读写的特点: 并发,将一个topic拆分多个partitio ...
- IDEA 入门
IDEA初步使用 IntelliJ IDEA 使用教程(2019图文版) -- 从入门到上瘾 IntelliJ IDEA 设置代码提示或自动补全的快捷键 (Alt+/) IntelliJ IDEA 配 ...
- p2596 书架(Treap)
写平衡树修锅快修到死系列 我太蠢了 其实是平衡树裸体裸题 插入,删除,交换前驱或后继,查询rank和kth 维护一个pos数组,表示第i个书的编号 然后注意许许多多的细节,没了 #include &l ...